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

Peter Hall peter.hall at memorphic.com
Thu Jul 31 09:34:00 PDT 2008


> Here's my take at an example of brittleness:
>
> var bar = 42;
>
> class foo {
>   function zot () { return bar; }
> }
>
> ...
>
> class subfoo extends foo {
>   var bar = 'oops!';
> }
>
> (new foo).zot() => 42
> (new myfoo).zot() => ?


In AS3, the reference to bar in the zot function would be bound to
this.bar so, even in the subclass, there is no ambiguity and both
cases would output 42. I assume that ES4 would follow this behaviour.

The fragility is more likely to be in the opposite situation, where a
method in a class intends to access a global variable, but the
superclass has declared it too.


Peter



More information about the Es4-discuss mailing list