Getting a reference to the global object in an unknown environment in 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));
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.