Sunday, April 12, 2009

Anonymous function's contexgt

var name = "window"; var f = (function() { alert(this.name); }); f(); //window f.name = "local"; f(); //still window, the function still run in the context of window f.call(f); //local var p = { name: "local" }; p.f = f; p.f(); //local f.call(p); //local (function() { alert(this); })(); //the defining context is window, it will output window (function() { alert(this); }).call({}); //the defining context is window, but call switch the context

No comments:

Post a Comment