javascript:alert([].sort.call(null)) == [object window] の謎

JavaScript 難しい。 ECMAScript5の "use strict" 時の [].sort.call(null) の挙動に関する議論
6
Atsushi Takayama @edvakf

あ、58ページに書いてあった。If the function code is strict code, set the ThisBinding to thisArg.

2010-02-17 16:42:00
shogo ohta @os0x

@edvakf そっちはcallでthisを明示してない場合です。良くある無名関数の実行などではthisはglobal objectになります。で、ここがstrictモードではnullです。

2010-02-17 16:42:41
Atsushi Takayama @edvakf

call したときはまだ thisArg が null のまま (これは ES3 から変更) だけど、entering function code で strict mode じゃなければ null を global にしろと書いてある。

2010-02-17 16:43:35
Atsushi Takayama @edvakf

strict mode なら thisArg をそのまま (global にしたり ToObject したりせずに) ThisBinding すると。

2010-02-17 16:45:31
Atsushi Takayama @edvakf

javascript:alert(function(){return this}.call()) は normal mode では window で strict mode だと undefined

2010-02-17 16:46:34
shogo ohta @os0x

あれ、nullじゃなくてundefinedかも…。

2010-02-17 16:46:55
shogo ohta @os0x

@edvakf Function.prototype.callを使っている場合、normal modeでもundefinedになるのでは?

2010-02-17 16:48:51
shogo ohta @os0x

いや、明示してなければならないか。function(){return this}.call()とfunction(){return this}.call(undefined)で違いがでるのかな。うーん。

2010-02-17 16:51:00
Atsushi Takayama @edvakf

@os0x 58ページの下のリストを見ていくと、strict なら thisArg をそのまま ThisBinding にして、そうじゃなければ、と続きます。なので normal mode では互換性が保たれます。

2010-02-17 16:53:49
shogo ohta @os0x

@edvakf なるほど、15.3.4.4 Function.prototype.callが呼ぶ13.2.1 [[Call]]のなかで、thisは10.4.3見ろとあるので、そうなりそうです。

2010-02-17 17:10:08