How to escape implicit 'with (this)' of a method body
P T Withington
ptw at pobox.com
Thu Jul 31 09:49:19 PDT 2008
On 2008-07-31, at 12:34EDT, Peter Hall wrote:
>> 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
I don't follow. There is no `this.bar` in the class where zot is
defined.
> 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.
That was my original example, which would also exhibit fragility if
the superclass is developed/evolves independently. In either case,
the fragility stems from the implicit (unreformed) `with this` in
method bodies.
More information about the Es4-discuss
mailing list