gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:55 25 4
gpt4 key购买 nike

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

console.log(5..foo())

这在 Chrome 中记录 5,但在 Firefox 中记录 0。

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

console.log(5..bar)

这在两个浏览器中都按预期记录了 5。

谁能解释这种行为,并可能建议如何重写第一个示例以使其在 Firefox 中像在 Chrome 中一样工作?

jsfiddle.net/V2sHg/2/

最佳答案

当使用“new Number(value)”但不直接使用“number”时,它在 FF 上对我有用:

尝试:

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

关于javascript - getter 返回函数时 Firefox 中的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com