The global object as the "global scope instance object"

Andreas Rossberg rossberg at google.com
Tue Jan 24 12:22:01 PST 2012


On 24 January 2012 20:24, Brendan Eich <brendan at mozilla.org> wrote:
> I am skeptical we will banish var, ever. Consider the object detection
> pattern:
>
>  if (!this.foo) {
>    var foo = function () {
>      ...
>    };
>  }
>
> No way to make this work with let.

Actually, it is possible if this is a check for a primordial, and you
move them to the prototype of global this and allow shadowing them.
Then you'd be able to write:

let foo = Object.getPrototypeOf(this).foo || function() { ... }

(I think we had something like this on the board once.) Or, in the
browser, if window was actually bound to that prototype, this
simplifies to:

let foo = window.foo || function() { ... }

I don't think compatibility can afford binding window to something
different than global this, though. (One could still pre-bind the
prototype to a different name, however.)

Of course, I sincerely hope that the module system will provide a more
pleasant replacement for this pattern. :)

/Andreas


More information about the es-discuss mailing list