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.
The "use strict" Directive The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.
You can enable the strict mode by declaring this in the top of your script/function. 'use strict'; When a JavaScript engine sees this directive, it will start to interpret the code in a special mode.
Strict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.
First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With