How to escape implicit 'with (this)' of a method body

P T Withington ptw at pobox.com
Wed Aug 6 05:22:05 PDT 2008


On 2008-08-01, at 05:13EDT, Peter Hall wrote:

>>>
>>> In AS3, the reference to bar in the zot function would be bound to
>>> this.bar
>>
>> I don't follow.  There is no `this.bar` in the class where zot is  
>> defined.
>>
>
> Sorry, I said that backwards. When class foo is compiled, there is no
> member called bar, so the reference is bound to the global variable.
> Defining a member variable called bar in the subclass does not affect
> that.

I think what everyone is saying is that my model of how references in  
methods are resolved is wrong.  There is no implicit `with (this)` in  
method bodies, the references are resolved lexically, not dynamically.

In either case, though, it seems there is no in-language way to refer  
to a global that is shadowed by an instance variable.  The offered  
solutions assume that the implementation creates a global object that  
can be used to refer to globals.

> I don't know how convention will develop in other environments but, in
> AS3, global variables are almost always declared in packages, and
> strict mode is on by default. The result is that you would always get
> a compiler error in these cases instead of an ambiguous reference.


What is the error?  I don't get an error with this code:

     var free = 'outer';

     class Top {
       function test () { return free; }
     }

     class Sub extends Top {
       var free = 'inner';

       override function test () { return free; }
     }

Perhaps this is just poor practice, but it is confusing that the two  
uses of `free` result in two different bindings.



More information about the Es4-discuss mailing list