x === x在没有NaN的情况下返回false是否有任何值? [英] Is there any value for what x === x returns false without NaN?
问题描述
x === x
是否有任何值返回 false
没有NaN?
Is there any value for what x === x
returns false
without NaN?
例如:
> x = 1
1
> x === x
true
> x = {}
{}
> x === x
true
> x = new Date()
Wed Nov 13 2013 15:44:22 GMT+0200 (EET)
> x === x
true
> x = NaN
NaN
> x === x
false
我看到唯一值 x === x
返回 false
是 isNaN(x)=== true
。
是否还有 x
的另一个值 x === x
返回 false
?我们欢迎官方参考!
Is there another value of x
for what x === x
returns false
? An official reference would be welcome!
推荐答案
两个相等的非 - NaN $之间的严格比较c $ c> 值将永远为真( SLaks的回答正确引用规范)。但是,表达式
x
可以在评估相等性时更改其值。使用访问者属性描述符(即属性获取者)时,属性访问可能会发生这种情况:
The strict comparison between two equal non-NaN
values will always be true (SLaks's answer correctly quotes the spec). However, it's possible for the expression x
to change its value during the evaluation of the the equality. This can happen with property access when using accessor property descriptors (i.e., property getters):
foo = {};
Object.defineProperty(foo, "bar", {
get: function() {
return Math.random();
}
})
foo.bar === foo.bar; // false
如果对全局对象执行此操作窗口
(或节点中的全局
),然后您可以看到 x === x
比较失败全局范围变量:
If you do this for the global object window
(or global
in Node), then you can see the x === x
comparison fail for a global-scope variable:
Object.defineProperty(window, "bar", {
get: function() {
return Math.random();
}
})
bar === bar; // false
这篇关于x === x在没有NaN的情况下返回false是否有任何值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!