JavaScript Globals

When JavaScript runs in a web browser or in Adobe Air the window object is effectively the same thing as the global object. While there are a few functions that have true global scope and which are not attached to the window object, you can effectively treat the window object as being the equivalent of the global object (most of the time).

The same is not always true when you run JavaScript in other places. In Node, window is not the place where all your global variables and functions get attached - instead they are attached to the global object. Other server side environments also often use the global object or may call it something different.

The problem is how to write code to make use of the global object by whatever name the current environment uses so that the same code can be used in multiple situations on different platforms.

One solution proposed by Nicholas Zakas that works on older versions of JavaScript is:

function getGlobal(){
return (function(){
return this;
var glob = getGlobal();

We could also write this as:

var glob = function(){
return (function(){
return this;

Running that from anywhere in our code will result in glob pointing at the global object provided that it runs using the pre 2011 version of JavaScript. It will fail to run at all if the newer version of JavaScript is used because there the parameter to call is not allowed to be null.

Another way that some people have used that works in post 2011 JavaScript as long as the pre 2011 version is also supported is:

var glob = Function('return this')();

This will work where both versions of JavaScript are supported because the code in the Function constructor will use the older version of JavaScript even if all the rest of the code uses the newer version unless the body of its content starts with "use strict";.

Even this variant will not work when we encounter an environment that only supports post 2011 JavaScript. I am not aware of any such environment but there is likely to be some appear in the future.

The following code will always work to obtain the global object in post 2011 JavaScript:

"use strict";
var get, glob;
get = eval;
glob = get('this');

This takes advantage of the fact that the new version of JavaScript treats all indirect calls to eval() as running in global scope. Unfortunately this will not work in pre-2011 JavaScript if the code is not all being run in global scope.

One proposal I have seen for combining these alternatives to get something that will always work is:

var glob = Function('return this')() || (1, eval)('this');

This variant effectively uses the Function constructor variant provided that it finds the global object and uses an indirect eval to find the global object if the first variant fails.

So why is getting a reference to the object that holds all of the global variables and functions so hard? Well you shouldn't really be using global scope at all in your JavaScript. By enclosing all of your code inside anonymous functions you give each script its own scope where it cannot interfere with other scripts. The changes that were made to JavaScript to make it easier to ensure that these separate scopes actually are separate and that you haven't accidentally assigned something to global scope have made it more difficult to actually reference global scope deliberately.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow