Is JavaScript's double equals (==) always symmetric?

谁说胖子不能爱 提交于 2019-12-30 01:35:09

问题


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?


回答1:


In Javascript, == is always symmetric.

The spec says:

NOTE 2 The equality operators maintain the following invariants:

  • A != B is equivalent to !(A == B).
  • A == B is equivalent to B == A, except in the order of evaluation of A and B.



回答2:


It's supposed to be symmetric. However, there is an asymmetric case in some versions of IE:

window == document; // true
document == window; // false



回答3:


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 to B == A, except in the order of evaluation of A and B.


来源:https://stackoverflow.com/questions/5669440/is-javascripts-double-equals-always-symmetric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!