2011年 04月 22日

JS の Function#toString の表現

そういやなんとなく不思議だと思っていたことがあって、 native コードを持つ Function オブジェクトの toString() メソッドを呼んだ結果の表現は、例えば Chrome だと

"".toString.toString() //=> "function toString() { [native code] }"

になるけど、ECMAScript 3rd Edition だと

An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration.

http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

とか書いてあって、実装依存とはいえ、FunctionDeclaration の形式にはなっているようにと読める。けど native コードの場合、雰囲気的に FunctionDeclaration っぽい感じではあっても、"[native code]" の部分は Statement として正しくないので、全体として FunctionDeclaration にはなってない (仕様として正しいの?)。

というかそもそも native コードを持つ Function オブジェクトの toString() メソッドを呼んだ結果の表現について仕様で明言されているのかを知りたかったんだけど、どっかに書いてあるのかな