[英]Is JavaScript's double equals (==) always symmetric?
There are many cases in which JavaScript's type-coercing equality operator is not transitive. 在许多情况下,JavaScript的类型强制相等运算符不可传递。 For example, see " JavaScript equality transitivity is weird ."
例如,请参阅“ JavaScript等同传递性很奇怪” 。
However, are there any cases in which ==
isn't symmetric ? 但是,是否存在
==
不对称的情况 ? That is, where a == b
is true
and b == a
is false
? 也就是说,
a == b
为true
且b == a
为false
?
It's supposed to be symmetric. 它应该是对称的。 However, there is an asymmetric case in some versions of IE:
但是,在某些版本的IE中存在不对称的情况:
window == document; // true
document == window; // false
In Javascript, ==
is always symmetric . 在Javascript中,
==
始终是对称的 。
NOTE 2 The equality operators maintain the following invariants:
注2:相等运算符维护以下不变量:
A != B
is equivalent to!(A == B)
.A != B
相当于!(A == B)
。A == B
is equivalent toB == A
, except in the order of evaluation ofA and B
.A == B
等于B == A
,除了A and B
的评估顺序。
The answer to your actual question (is the operator symmetric) is yes. 您的实际问题(操作符对称)的答案是肯定的。 The ECMA-262 spec explicitly states:
ECMA-262规范明确规定:
NOTE 2 The equality operators maintain the following invariants: 注2:相等运算符维护以下不变量:
A != B
is equivalent to !(A == B)
. A != B
相当于!(A == B)
。 A == B
is equivalent to B == A
, except in the order of evaluation of A
and B
. A == B
等于B == A
,除了A
和B
的评估顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.