2

我在 Float32Array 中添加了 3 个名为“x”、“y”和“z”的属性。getter 在 chrome 和 firefox 中都可以正常工作,但似乎 setter 只能在 chrome 中工作。这是为什么?它是一个错误吗?有没有办法让它在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;
4

2 回答 2

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

被引用为安全漏洞的原因(受影响的 twitter)在此处
的 chromium 中对此进行讨论

于 2012-12-14T09:56:58.433 回答
0

这里发生的是 Firefox 只是不允许在类型化数组上设置任何非数字属性。任何此类集合都将被忽略。在搜索原型链之前它们会被忽略,这就是为什么你的原型上的设置器没有被调用。

我不清楚每个规范的正确行为是什么;在这方面已经改变了几次。

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

于 2012-12-15T17:34:19.957 回答