Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

Use self?! #3

Closed
annevk opened this issue Nov 20, 2015 · 6 comments
Closed

Use self?! #3

annevk opened this issue Nov 20, 2015 · 6 comments

Comments

@annevk
Copy link
Member

annevk commented Nov 20, 2015

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
Copy link

@annevk FF extensions.

@ljharb
Copy link
Member

ljharb commented Nov 20, 2015

@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
Copy link
Member Author

annevk commented Nov 21, 2015

Hmm, meh.

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

@ljharb
Copy link
Member

ljharb commented Nov 21, 2015

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
Copy link
Contributor

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
Copy link
Member

ljharb commented Mar 28, 2016

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.

@ljharb ljharb closed this as completed Mar 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants