Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getter返回函数时Firefox中的意外行为_Javascript_This_Getter_Prototype Programming - Fatal编程技术网

Javascript getter返回函数时Firefox中的意外行为

Javascript 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

这在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中工作,就像在Chrome中一样


在FF上使用“新数字(值)”但不直接使用“数字”时,它对我有效:

尝试:


我想不出来,但可能Firefox在获取属性时不想返回函数。确实是非常奇怪的行为-Firefox在尝试获取函数时似乎将数字原型绑定到了
这个
,但删除了
()
after
5..foo
将其绑定到
Number
的实例。也许是个虫子?
Object.defineProperty(Number.prototype, 'bar', {
  get: function () {
    return this.valueOf()
  }
})

console.log(5..bar)
var n = new Number(8);
n.foo(); --> 8