tc39 / proposal-global

ECMAScript Proposal, specs, and reference implementation for `global`
http://tc39.github.io/proposal-global/
MIT License
349 stars 18 forks source link

Use self?! #3

Closed annevk closed 8 years ago

annevk commented 8 years ago

I don't understand why it would be global or window, when only self works across documents and workers in browsers.

(And ECMAScript should be updated to let HTML state what the global object actually is and what this returns, etc. I filed a bug on that a long time ago...)

zloirock commented 8 years ago

@annevk FF extensions.

ljharb commented 8 years ago

@annevk it doesn't work in node, and there's lots of code that detects the environment and would break if we used any of the pre-existing global names. Per https://github.com/tc39/proposal-global#naming, I'm going to do the research and bring it to the committee - if it will break code, it will be System.global, if it won't, global is the best option.

Also, var self is a very common usage pattern, and if someone forgets var it will shadow the global variable right now - but this proposal would make it nonwritable, so that would also likely break the web.

annevk commented 8 years ago

Hmm, meh.

Just make sure the host language can control it I guess since we cannot expose the real global for document environments.

ljharb commented 8 years ago

This proposal uses the same language as the rest of the spec, so it has no effect on any preexisting constraints about using the real global versus a proxy. See also this note in the readme.

I'll leave this open until the naming issue is resolved, since although I'm supremely confident that using any of the existing names will break the web (sadly), I haven't yet done the research to prove it :-)

dead-claudia commented 8 years ago

I believe, according to spec, that scripts are loaded sloppy by default. This makes top-level this the best variable to pull from for the global. The only way currently to reliably get sloppy mode in a module is through Function("return this")().

ljharb commented 8 years ago

Regarding the naming issue:

I've done some research, and there's a number of places where typeof global is used to determine if the environment is "node" or not - in particular, that the presence of global implies that both exports and the process module are present. This convinces me that global is not an option, since there exists code that runs in both a node and browser context that already assumes the the presence of global implies "node".

Similarly, there's a number of places where typeof self === 'undefined' is being used to indicate that an env is node, in code that runs both in browsers and in node.

As such, all three of self and window and global are not options, because they would break code on the web.