2012-12-14 32 views
2

我已经为Float32Array添加了3个名为'x','y'和'z'的属性。 getter在chrome和firefox都可以正常工作,但它似乎只能在chrome中使用。这是为什么?这是一个错误吗?有没有办法让它在Firefox中工作?Float32Array设置属性不能在Firefox中工作

Object.defineProperty(Float32Array.prototype, 'x', { 
    get: function(){ 
     return this[0]; 
    }, 
    set: function(x){ 
     this[0] = x; 
    } 
}); 


// creating a Float32Array-Vector using mjs.js 
var vector = V3.$(1,2,3); 

// works fine 
document.writeln(vector.x); 

// works in chrome but not in firefox 
vector.x = vector.y + vector.z; 

回答

1

我发现这个问题非常有趣,并已研究它。我能够重现您遇到的问题。 setter永远不会被调用,但getter被调用。在探索,发现了以下文字:

JavaScript 1.8.1 note 
Starting in JavaScript 1.8.1, setters are no longer called when setting properties in object and array initializers. 

看的网址:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects

也有更多的参考资料:https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.8.1

而且https://dev.mozilla.jp/localmdc/localmdc_11696.html

原因列为安全漏洞(影响叽叽喳喳)
按照铬中的讨论here

+0

感谢您的研究。我没有得到的是,我没有在对象或数组初始值设定项中设置属性,所以如果这真的不起作用? – Markus

0

这里发生的事情是,Firefox不允许在类型数组上设置任何非数字属性;任何这样的组都被忽略。在搜索原型链之前它们被忽略,这就是为什么原型上的setter没有被调用。

我不清楚这里的正确行为是按照规范进行的;它在这个领域改变了几次。

备注https://bugzilla.mozilla.org/show_bug.cgi?id=695438