How to escape implicit 'with (this)' of a method body
Jon Zeppieri
jaz at bu.edu
Mon Jul 28 22:05:45 PDT 2008
2008/7/29 Brendan Eich <brendan at mozilla.org>:
> 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
Isn't the 'with' statement in the original example significant? In
the general case, assuming that you don't know what properties 'this'
has (as it may have dynamic properties in addition to the fixtures
determined by its class), you have no way of knowing whether 'global'
or 'window' refers to the global object or to some arbitrary property
of 'this.'
-Jon
>
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>
>
More information about the Es4-discuss
mailing list