arguments.callee
初めて知ったので callee - Mozilla Developer Center を読む。
Specifies the currently executing function.
spidermonkey で試してみる。
js> function test() { print(arguments.callee); } js> test(); function test() { print(arguments.callee); }
arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.
はー。なるほど。そういう場面で使うのか。
js> (function(n){return n > 0 ? n + arguments.callee(n-1) : n})(10) 55
うーん。これはすごい。
The this keyword does not refer to the currently executing function. Use the callee property to refer to a function within the function body.
js> (function(){print(this)})() [object global]
グローバルオブジェクト?なるものが this に設定されているらしい。グローバルな名前空間をもつオブジェクトってことなのかな。
調べてみたら、下のように書いてあった。
The global object itself can be accessed by this in the global scope.
Core JavaScript 1.5 Reference - Mozilla Developer Center
てことは、普通に関数を実行する場合は、関数内での this は、グローバルスコープの this を参照してるってことなのかな。
js> a = 3 3 js> this.a 3 js> (function(){print(this.a)})() 3 js> function test() { this.a=11; print(this.a) } js> test() 11 js> a 11