global object in strict mode

John J Barton johnjbarton at johnjbarton.com
Fri Aug 24 11:19:46 PDT 2012


On Fri, Aug 24, 2012 at 10:51 AM, Brendan Eich <brendan at mozilla.org> wrote:
> 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).

CSP creates another JS environment by banning certain JS features.

>
> 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"?

I guess the purpose of "global" is to document dependency on the JS
global objects but not on the rest of the global objects attached to
|window|.

(To your earlier question about the 'problem', my problem is simply to
use some else's JS code in a CSP environment. It uses the eval form. I
need to convince them of an alternative. But it seems like this will
be a common problem, hence my post).

>
> 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.

Perhaps that is the reason for the funky construct,
 var global = ("global", eval)("this");
http://perfectionkills.com/global-eval-what-are-the-options/


>
> 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
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list