Question

What is the recommended way to get a handle to the global object in ES5 strict mode in an unknown host environment?

ECMAScript doesn't provide a built-in way to reference the global object that I'm aware of. If it does, this is the answer I'm looking for.

In a known environment, the global object usually has a self-referential property. Since the global object is the VO for the global scope, properties of the global object are global variables, so we can use them get a handle to the global object from anywhere:

  • In a web browser, we can use window or self.

  • In node.js, we can use global.

However, this is not necessarily the case in all host environments. As far as I know, Windows Script Host does not provide any way to access the global object. The recommended way to get the global object in WSH seems to be to use the this keyword in a context where it does not resolve to an object. For example:

var GLOBAL = (function(){return this}());

This technique will work for any host environment, but not in strict mode, because an undefined this does not reference the global object in strict mode:

If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4).

As expected, the following code results in undefined:

(function(){
    "use strict";
    var GLOBAL = (function(){return this}());
    console.log(GLOBAL);
}());

So, what is the proper way to get a handle to the global object in any environment, regardless of strict mode?

By the way, my current approach is to sniff for global variables referencing the global object like this:

var self, window, global = global || window || self;

...and then just use global. I think this is a bad solution for a number of reasons, most of which are fairly obvious, and it doesn't address the WSH problem.

Was it helpful?

Solution

In ES5, you can get a reference to global object from within strict mode via indirect eval call:

"use strict";
var global = (1,eval)('this');

Take a look at my article; particularly at this section on strict mode.

OTHER TIPS

In global code, the thisBinding is set to the global object regardless of strict mode. That means you can pass it from there into your module IEFE:

// "use strict"; or not
(function(global) {
    "use strict";
    …
    console.log(global);
    …
}(this));

In strict mode, the way to get a reference to the global object is to assign a variable in the global object referencing itself.

That is this means the global object when in the global context, so the solution is simply:

"use strict";
var global = global || this;
(function() { global.hello = "world"; })();
console.log(hello); // Outputs 'world' as expected

This does mean that you have to pollute the global namespace with a reference to itself, but like you say, it should already have been there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top