Closed ljharb closed 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:
For posterity, a twitter poll: https://twitter.com/ljharb/status/734450903059570688
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:
self === window
. No breaking changes possible.self
is the global object in web workers / service workersFrom 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.
@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.
@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.
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.
however, the problem is with node scripts that assume a browser environment if self
exists.
Document cases where using
self
,window
, orglobal
would break code that is currently using it for environment detection.