Heim > Fragen und Antworten > Hauptteil
[1] == [1] Die Anzahl der gedruckten Werte ist falsch. Weiß jemand warum?
过去多啦不再A梦2017-05-19 10:46:27
比较操作符中的相等运算符,有详细的介绍:
相等(==)
比较操作符会为两个不同类型的操作数转换类型,然后进行严格比较。当两个操作数都是对象时,JavaScript会比较其内部引用,当且仅当他们的引用指向内存中的相同对象(区域)时才相等,即他们在栈内存中的引用地址相同。
楼上说的都对,因为[1] == [1]中,两个数组是不同的对象,所以不相等。
習慣沉默2017-05-19 10:46:27
Ecma-262.pdf
7.2.13 Abstract Equality Comparison
已经明确说明了,这里的 [1] = [1]
实际是 [1] === [1]
正好试用第一条。
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then a. Return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x ==
ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x)
== y.
Return false.