0
votes

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?

1
Yeah, I can't make sense of that statement either. In my experience this is always global within a nested function. FYI if you trace this you'll get [object global] when its global.Aaron Beall
You should never use "this". Don't use it because, in most cases, it's being used improperly or without really knowing what it means. Not using "this" forces you to structure your code correctly. It's like the rule about using "parent" or nesting functions inside of functions.moot

1 Answers

0
votes

I believe the case is the reference to the Object, not the nested function concept.

If you try:

 function f(target:Object):void
 {
     trace("f() this.a", target.a); // "ok"
     function g():void { trace("g() this.a", target.a); } // "ok"
     g();
 }

 f( { a: "ok" } );