假设我有数组 a。
var a = [1,2,3];
然后是数组b。
var b = [1,2,3];
a == b
是否有可能返回 true?即使是百万分之一。 (我知道通常 a == b
为假,但我想知道它是否有可能为真。)
最佳答案
规范的相关部分是11.9.3 The Abstract Equality Comparison Algorithm :
The comparison
x == y
, wherex
andy
are values, producestrue
orfalse
. Such a comparison is performed as follows:
- If Type(x) is the same as Type(y), then
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same Number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
- If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false.
(强调我的。)
所以你的问题的答案是否定的。 a == b
仅当它们实际上是同一个对象时才返回 true
。
关于javascript - javascript 中的两个对象/数组会巧合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127967/