global object in strict mode

Brendan Eich brendan at mozilla.org
Fri Aug 24 10:41:30 PDT 2012


John J Barton wrote:
> Thanks!
>
>   I guess I should ended my question this way: ...and because
> developers want to write code that works in browsers and not, perhaps
> ES can have a solution that is not as silly as the ones we are using
> now.

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?

But really, what's wrong with the old chestnut of capturing the global 
early at top level?

/var global = this;/


and using that name, or passing its value around, as needed?

It's not as if one must be able to utter an expression deep in a 
function nest to get a global -- or as if there is any identifier that 
might not be shadowed. We are not going to add a special syntactic form 
for "the global". So I don't think the solutions available or "silly", 
or the problem is exactly what this thread makes it out to be.

But possibly I misunderstood the problem.

/be

>
> jjb
>
> On Thu, Aug 23, 2012 at 6:41 PM, Andrea Giammarchi
> <andrea.giammarchi at gmail.com>  wrote:
>> the non eval solution for ES3 is
>>
>> var global = function(){return this}();
>>
>> and not with eval ...
>>
>> In ES5 usually window is not writable/configurable but enumerable so
>> theoretically you can rely in the `window` object unless your code is not
>> evaluated through eval inside a function with a window variable redefined.
>>
>> On Thu, Aug 23, 2012 at 11:59 PM, John J Barton
>> <johnjbarton at johnjbarton.com>  wrote:
>>> Long ago this list had a subject:
>>>
>>> How to retrieve the global object in strict mode?
>>> https://mail.mozilla.org/pipermail/es5-discuss/2011-February/003919.html
>>>
>>> Roughly the conclusion was:
>>>
>>>    var global = ("global", eval)("this");
>>>
>>> However Content Security Policy
>>>
>>>
>>> https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html
>>>
>>> bans 'eval', (as well as all similar forms) making this solution fail.
>>>
>>> Is there a non-eval based solution?
>>>
>>> jjb
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120824/bce3c42c/attachment.html>


More information about the es-discuss mailing list