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

Document detailed naming research #11

Closed ljharb closed 8 years ago

ljharb commented 8 years ago

Document cases where using self, window, or global would break code that is currently using it for environment detection.

ljharb commented 8 years ago

There's a number of examples of window and self both implying a browser, such as https://github.com/Microsoft/automatic-graph-layout/blob/ab371280000f4d85eeedc1899e6faaa66ccad009/GraphLayout/MsaglSharpkit/WebMsagl/Msagl/sharpkit_pre.js . In addition, var self = this is too common a pattern for self to be a safe bet.


global is a bit less problematic, since browserify automatically transpiles global to window, but here are some example places where global implies node, and node globals like module, process, etc:

ljharb commented 7 years ago

For posterity, a twitter poll: https://twitter.com/ljharb/status/734450903059570688

pladaria commented 5 years ago

Can this be re-opened?

To me there are no strong reasons to discard self.

There's a number of examples of window and self both implying a browser, such as...

Could you provide a list of those examples? (provided example makes no usage of self). If those examples run in backend, to me that's a minor issue.

var self = this

I've never seen that in modern (ES6+) JS


Pro self reasons:

From mdn docs:

By using self, you can refer to the global scope in a way that will work not only in a window context (self will resolve to window.self) but also in a worker context (self will then resolve to WorkerGlobalScope.self).

So, looks like making self === global in node (or other non-browser runtimes) is the next logical step.

kleinfreund commented 5 years ago

@pladaria A list of examples is provided in https://github.com/tc39/proposal-global/issues/11#issuecomment-220802575.

I've never seen that in modern (ES6+) JS

I see var self = this; all the time. As for sites that would break, there is a substantial number of live examples that demonstrate this; hence, it is not being considered anymore.

j-f1 commented 5 years ago

@kleinfreund Creating a self variable shouldn’t cause an issue since the self will shadow the global self. Again, self already exists on the web, just not on Node.

obedm503 commented 5 years ago

I agree with @j-f1. if a variable self is defined as something else, js already treats it as normal. Scoping rules apply as usual.

image

however, the problem is with node scripts that assume a browser environment if self exists.