Javascript getter返回函数时Firefox中的意外行为
这在Chrome中记录为5,但在Firefox中记录为0Javascript getter返回函数时Firefox中的意外行为,javascript,this,getter,prototype-programming,Javascript,This,Getter,Prototype Programming,这在Chrome中记录为5,但在Firefox中记录为0 Object.defineProperty(Number.prototype, 'foo', { get: function () { var me = this return function () { return me.valueOf() } } }) console.log(5..foo()) 这将按预期在两个浏览器中记录5 有人能解释一下这种行为吗?也许有人能建议如何将第一个示例改写为在Firefox
Object.defineProperty(Number.prototype, 'foo', {
get: function () {
var me = this
return function () { return me.valueOf() }
}
})
console.log(5..foo())
这将按预期在两个浏览器中记录5
有人能解释一下这种行为吗?也许有人能建议如何将第一个示例改写为在Firefox中工作,就像在Chrome中一样
在FF上使用“新数字(值)”但不直接使用“数字”时,它对我有效: 尝试:
我想不出来,但可能Firefox在获取属性时不想返回函数。确实是非常奇怪的行为-Firefox在尝试获取函数时似乎将数字原型绑定到了
这个,但删除了()
after5..foo
将其绑定到Number
的实例。也许是个虫子?
Object.defineProperty(Number.prototype, 'bar', {
get: function () {
return this.valueOf()
}
})
console.log(5..bar)
var n = new Number(8);
n.foo(); --> 8