The global object in JavaScript is vitally important: all global variables and functions become properties fo the global object. In browsers. the window object doubles as the global object, and most developers use it as such without even realizing. In other JavaScript environments, however, the global object is something else. Most of the time, it’s not assigned to a global variable for you to access.

If you code is to run in non-browser JavaScript environments, you’d better avoid using window for dealing with globals. However, referencing the global object can be necessary. To that end, I present the getGlobal() function, which works in any JavaScript environment and always returns the global object:

function getGlobal(){
return (function(){
return this;
}).call(null);
}

The key to this function is that the this object always points to the global object anytime you are using call() or apply() and pass in null as the first argument. Since a null scope is not valid, the interpreter inserts the global object. The function uses an inner function to assure that the scope is always correct. You can then use this function as follows:

var global = getGlobal();

And I suggest you do this whenever writing JavaScript that should be executable in non-browser environments. Enjoy.

Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.

Both comments and pings are currently closed.