How to escape implicit 'with (this)' of a method body
Brendan Eich
brendan at mozilla.org
Mon Jul 28 22:02:59 PDT 2008
On Jul 28, 2008, at 9:46 PM, Brendan Eich wrote:
> On Jul 28, 2008, at 3:22 PM, Michael Haufe wrote:
>> function foo () { return 'global'; }
>>
>> class bar {
>> function foo () { return 'local'; }
>>
>> function zot () {
>> // How can I call the global foo from here?
>> without (this) { foo(); }
>> }
>> }
>
> It's the same as if you lambda-coded the above (here shown in JS1.8
> [Firefox 3], note the expression closures):
>
> function bar() {
> function foo() 'local';
> function zot() global.foo();
+ return {foo: foo, zot: zot};
> }
> function foo() 'global';
>
> This example uses ES4's global synonym for the global object, but
> you could capture this in a global var at top level:
>
> var global = this;
> print(new bar().zot()); // print 'global'
>
> in ES3 or JS1.8 to get the same effect.
/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080728/7d56fc2f/attachment-0002.html
More information about the Es4-discuss
mailing list