JavaScript 的双等号 (==) 总是对称的吗?
在很多情况下,JavaScript 的类型强制相等运算符是不可传递的。例如,请参阅“JavaScript 相等传递性很奇怪”。
但是,是否存在 ==
不对称的情况?也就是说,其中 a == b
为 true
,而 b == a
为 false
?
There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see "JavaScript equality transitivity is weird."
However, are there any cases in which ==
isn't symmetric? That is, where a == b
is true
and b == a
is false
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是对称的。然而,在某些版本的IE中存在不对称的情况:
It's supposed to be symmetric. However, there is an asymmetric case in some versions of IE:
在 Javascript 中,
==
始终是对称。规范说 :
In Javascript,
==
is always symmetric.The spec says:
您的实际问题(运算符是否对称)的答案是肯定的。 ECMA-262 规范明确指出:
注意2 相等运算符保持以下不变量:
A != B
等价于!(A == B)
。A == B
等同于B == A
,除了A
和B
的计算顺序不同。The answer to your actual question (is the operator symmetric) is yes. The ECMA-262 spec explicitly states:
NOTE 2 The equality operators maintain the following invariants:
A != B
is equivalent to!(A == B)
.A == B
is equivalent toB == A
, except in the order of evaluation ofA
andB
.