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

Brendan Eich brendan at mozilla.org
Mon Jul 28 21:46:13 PDT 2008


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();
}
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.

> You could use window["foo"](); or whatever the global object is  
> named in the environment

No need to quote and bracket, of course -- window.foo() is fine too.

/be

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080728/44ff5516/attachment-0002.html 


More information about the Es4-discuss mailing list