更新时间:2023-11-29 10:13:22
您可以在ECMA规范中查找比较算法(ECMA-262的相关章节,第3版针对您的问题:11.9.3, 9.1,8.6.2.6)。
You can look up the comparison algorithm in the ECMA-spec (relevant sections of ECMA-262, 3rd edition for your problem: 11.9.3, 9.1, 8.6.2.6).
如果将所涉及的抽象算法转换回JS,那么在评估 2 == [2]时会发生什么? / code>基本上是这样的:
If you translate the involved abstract algorithms back to JS, what happens when evaluating 2 == [2]
is basically this:
2 === Number([2].valueOf().toString())
其中 valueOf()
对于数组返回数组本身,单元素数组的字符串表示形式是单个元素的字符串表示。
where valueOf()
for arrays returns the array itself and the string-representation of a one-element array is the string representation of the single element.
这也解释了第三个例子为 [[[[[[[2]]]]]]]。toString()
仍然只是字符串 2
。
This also explains the third example as [[[[[[[2]]]]]]].toString()
is still just the string 2
.
As你可以看到,有相当多的幕后魔术,这就是为什么我一般只使用严格的相等运算符 ===
。
As you can see, there's quite a lot of behind-the-scene magic involved, which is why I generally only use the strict equality operator ===
.
第一个和第二个例子更容易理解,因为属性名称总是字符串,所以
The first and second example are easier to follow as property names are always strings, so
a[[2]]
相当于
a[[2].toString()]
只是
a["2"]
请记住,在任何数组魔法发生之前,偶数数字键也会被视为属性名称(即字符串)。
Keep in mind that even numeric keys are treated as property names (ie strings) before any array-magic happens.