简体   繁体   English

JavaScript的双等于(==)总是对称的吗?

[英]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 == btrueb == afalse

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中, ==始终是对称的

The spec says : 规范说

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 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 ,除了AB的评估顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM