参考にしたページはこちらです。
- http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
問題
[] == ![] // これはなんですか?
道のり1
まず、右辺が考えやすいので、右辺を考えます。
Boolean([]) // => true
ですので、
![] // => false
となります。よって
[] == false
道のり2
ここで 11.9.3 The Abstract Equality Comparison Algorithm によります。 ※ 以下、何度か参照します。
7: If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
上の項目に該当すると思います。それに沿って右辺を number にします。
Number(false) // => 0
ですので、次のように書き換えれます。
[] == 0
道のり3
続いて
9: If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
上に該当します。
ここで、挫折しました。 ToPrimitive(x)
が全然分かりません。
しばらく迷子になった後、
以下の記述を Equality comparisons and sameness に出会いました。
ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.
「ToPrimitive(A)は、 toString や valueOf メソッドをつかって、 A を primitive な値に変換しようとする。」と解釈しました。
ですので、以下の操作から、 [] の primitive値は "" となります。
[].valueOf() // => [] これは primitive じゃない
[].toString() // => "" これは primitive です
したがって、左辺を primitive にして以下のようになります。
"" == 0
道のり4(おしまい)
最後に、
5: If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
こちらに従うことで、
Number("") // => 0
0 == 0 // => true
以上です。ありがとうございました。