How to get the global object in JavaScript?

Well, you can use the typeof operator, and if the identifier doesn’t exist in any place of the scope chain, it will not throw a ReferenceError, it will just return "undefined":

if (typeof ModuleName != 'undefined') {
  //...
}

Remember also that the this value on Global code, refers to the global object, meaning that if your if statement is on the global context, you can simply check this.ModuleName.

About the (function () { return this; }()); technique, you are right, on strict mode the this value will simply be undefined.

Under strict mode there are two ways to get a reference to the Global object, no matter where you are:

  • Through the Function constructor:

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

Functions created with the Function constructor don’t inherit the strictness of the caller, they are strict only if they start their body with the 'use strict' directive, otherwise they are non-strict.

This method is compatible with any ES3 implementation.

  • Through an indirect eval call, for example:

    "use strict";
    var get = eval;
    var global = get("this");
    

The above will work because in ES5, indirect calls to eval, use the global environment as both, the variable environment and lexical environment for the eval code.

See details on Entering Eval Code, Step 1.

But be aware that the last solution will not work on ES3 implementations, because an indirect call to eval on ES3 will use the variable and lexical environments of the caller as the environments for the eval code itself.

And at last, you may find useful to detect if strict mode is supported:

var isStrictSupported = (function () { "use strict"; return !this; })();

Leave a Comment