我已将名为“x”、“y”和“z”的 3 个属性添加到 Float32Array。 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;
最佳答案
我发现这个问题很有趣,并且研究了它。我能够重现您遇到的问题。 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
引用为安全漏洞的原因(影响推特)
在 chromium 中关注此问题的讨论 here
关于javascript - Float32Array 设置属性在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13875191/