typeof null === 'object' は ECMAScript 3 の仕様バグ
ECMAScript 3 の typeof 演算子
typeof演算子は対象の型を返す演算子ですが、null に適用すると "object" が返ってきます。
console.log(typeof null === 'object'); // true
では、null は Object 型なのか、というとそうではなくて仕様バグだったりします。
#250 ((Resolved) "typeof null") – ECMAScript Bugs – TracChanged 3 weeks ago by brendan
You know, this all came about because of rushing in early May 1995, which led to a leak of type tag representation shared by null and object types. But null means "no object", so it didn't raise hackles until it was too late to fix in Netscape 2, and after that we were loath to "fix" it and "break the web".
That argument only applies more in degree of web population now.
We have other fish to fry. This one was has been swallowed already. Let's not change typeof null for ES4 and work on more vital issues.
/be
簡単に説明すると、「納期に押されてやっちまったぜ!」的なコメントだそうで…。
ECMAScript 5.1 の typeof 演算子
ECMAScript 5.1 でも typeof 演算子の挙動は変わりませんが、Object型 の解説が寄り詳しくなったので意訳してみました。
型 | typeof 演算子の結果 |
---|---|
Undefined型 | "undefined" |
Null型 | "object" |
Boolean型 | "boolean" |
Number型 | "number" |
String型 | "string" |
Object型 (ネイティブオブジェクトで Call を持たないもの) | "object" |
Object型 (ネイティブオブジェクトで Call を持つもの) | "function" |
Object型 (ホストオブジェクト) | "undefined", "boolean", "number", "string" を除く処理系定義 |
ES.next の typeof 演算子
ECMAScript 5.1 の後継に ES.next があり、上手くいけば ECMAScript 6 として仕様策定される見込みとなっています。
ES.next ではとうとう typeof 演算子の仕様バグが修正されました。
console.log(typeof null === 'null'); // true