global object in strict mode

Brendan Eich brendan at mozilla.org
Fri Aug 24 10:51:58 PDT 2012


Kris Kowal wrote:
> On Fri, Aug 24, 2012 at 10:41 AM, Brendan Eich<brendan at mozilla.org>  wrote:
>> I'm not sure what the problem is -- I read the old thread, and noticed the
>> solution:
>> var global = Function("return this")();
>> This is good for any code mode, strict or non-strict. Does CSP ban Function
>> as well as eval?
>
> CSP does forbid the Function constructor, by the edict “Code will not
> be created from strings”.
>
> http://www.w3.org/TR/CSP/ Section 4.2 “If unsafe-eval is not allowed…”

Sure, makes sense (I think I even knew that once -- have to catch up on 
CSP when I have some time, next millennium :-P).

Is it common to want an expression, usable in any context (non-strict, 
strict, CSP, deep in a function nest with potentially many names in 
scope, some of which might shadow globals), that evaluates to "the 
current global object"?

JS libraries do things like

(funciton (global) {
   // all the code here
})(this);

and that works, as well as the brute force

var global = this;

approach. But one must take care not to shadow the name.

Could ES6 add a predefined global property named 'global', set to 
reference the global object? I suppose maybe - it would be writable or 
(to use WebIDL's term) [Replaceable]. We can't just make a const global, 
we will break extant code.

Is this global global important to standardize?

/be


More information about the es-discuss mailing list