I'm trying to define the global
object in JavaScript in a single line as follows:
var global = this.global || this;
The above statement is in the global scope. Hence in browsers the this
pointer is an alias for the window
object. Assuming that it's the first line of JavaScript to be executed in the context of the current web page, the value of global
will always be the same as that of the this
pointer or the window
object.
In CommonJS implementations, such as RingoJS and node.js the this
pointer points to the current ModuleScope
. However, we can access the global
object through the property global
defined on the ModuleScope
. Hence we can access it via the this.global
property.
Hence this code snippet works in all browsers and in at least RingoJS and node.js, but I have not tested other CommomJS implementations. Thus I would like to know if this code will not yield correct results when run on any other CommonJS implementation, and if so how I may fix it.
Eventually, I intend to use it in a lambda expression for my implementation independent JavaScript framework as follows (idea from jQuery):
(function (global) {
// javascript framework
})(this.global || this);
Implementation independant version is not trivial
Your going to have to hard code in feature detection for every environment.
After reading Esailija and Raynos' answers I understood that my code
this.global || this
will not work for all cases in node.js; and that it may even fail in browsers if a variable calledglobal
already exists in the global scope.Esailija pointed out that
this.global
is not really theglobal
object, stating instead thatthis
is theglobal
object in RingoJS; and although I understand his arguments, for my purposes I requirethis.global
and notthis
.Raynos suggested that I hard code feature detection for every CommonJS environment. However since I'm currently only supporting RingoJS and node.js, I only need to test for
global
andwindow
. Hence I decided to stick withthis.global || this
.Nevertheless, as I said before
this.global || this
doesn't work for all cases in node.js as I understood from benvie's comments. In the node.js REPL I realized that I requirethis
and notthis.global
. However,this.global || this
expressesthis.global
. In a node.js module I requirethis.global
and notthis
. However, it expressesthis
sincethis.global
isundefined
. Hence to solve this problem I finally decided to use the following code:The reason I'm using this code is because in node.js modules
this.global
isundefined
. Hence we must useglobal
directly. Thus we usetypeof global !== "undefined" && global
to get theglobal
object in both RingoJS and node.js; and we usethis
as theglobal
object in browsers (window
) and as a default fallback.Note: I didn't provide any logic for finding the
global
object in the node.js REPL because I don't believe that my framework will be used directly within the REPL anyway. However, writing the logic to find it should be fairly trivial once one understands the complications of finding theglobal
object in node.js as benvie pointed out. I know that I don't.this
is in no way relevant to scope.this
is only resolved during the call time of the function and comes down to few simple rules.this
inside the functionthis
will be undefined so under non strict mode it will be the global object.call/.apply
thenthis
is explicitly set by yourself.So as you can see, it would fall under rule #2, which resolves to
undefined
. And since there is no"use strict";
:Edit: I have now ran some quick tests in RingoJS and they infact put the "global object" inside the actual global object (as defined by standards) which is
ModuleScope
. Just because the actual global object in most js implementations has Object and String and so on, doesn't make an object global if it has those objects under it as well. The reason you can accessString
andObject
in RingoJS is because they put them into theModuleScope
prototype:Further proof that
ModuleScope
is the actual global object:So nothing is gained from this kind of trickery, it doesn't fix anything:
Rant over,
this
refers to the actual global object already according to the rules given earlier in this post.this.global
is not the real global object as defined by standards, it's just a container.Additionally you could emulate this behavior in browsers:
Consider scopehack.js
Consider main.html:
And finally a "module" module.html:
Which one is an actual global object in both module.html and main.html? It is still
this
.TLDR:
Does not make
obj
the global object.