简体   繁体   English

当getter返回函数时,Firefox中出现意外行为

[英]Unexpected behaviour in Firefox when getter returns a function

Object.defineProperty(Number.prototype, 'foo', {
  get: function () {
    var me = this
    return function () { return me.valueOf() }
  }
})

console.log(5..foo())

This logs 5 in Chrome, but 0 in Firefox. 这在Chrome中记录5,在Firefox中记录0。

Object.defineProperty(Number.prototype, 'bar', {
  get: function () {
    return this.valueOf()
  }
})

console.log(5..bar)

This logs 5 in both browsers as expected. 这会按预期在两个浏览器中记录5。

Can anyone explain this behaviour, and perhaps suggest how the first example could be rewritten to work in Firefox as it does in Chrome? 任何人都可以解释这种行为,并且可能会建议如何将第一个示例重写为在Firefox中工作,就像在Chrome中一样?

jsfiddle.net/V2sHg/2/ jsfiddle.net/V2sHg/2/

It works for me on FF when using "new Number(value)" but not using directly a "number": 当使用“new Number(value)”但不直接使用“数字”时,它适用于FF

Try: 尝试:

var n = new Number(8);
n.foo(); --> 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM