ActionScript 3 language specification states:
In ECMA-262 edition 3, when this appears in a nested function, it is bound to the global object if the function is called lexically, without an explicit receiver object. In ActionScript 3.0, this is bound to the innermost nested this when the function is called lexically.
(Source: http://help.adobe.com/livedocs/specs/actionscript/3/wwhelp/wwhimpl/js/html/wwhelp.htm)
However I tried the following and my result is not what I expected from the sentence above - the this
inside the nested function is bound to the global object:
function f():void
{
trace("f() this.a", this.a); // "ok"
function g():void { trace("g() this.a", this.a); } // "undefined"
g();
}
f.call( { a: "ok" } );
Either the documentation is wrong here or I didn't understand it correctly. Can you explain me?
this
is always global within a nested function. FYI if you tracethis
you'll get[object global]
when its global. – Aaron Beall