Assigning to globals in strict mode

David Herman dherman at mozilla.com
Wed Oct 20 08:09:01 PDT 2010


If I could just flog the global object a bit more: there are lots of way in which interleaved dynamic control can make it statically unpredictable whether a global variable will exist at the point where its reference is evaluated.

You might or might not dynamically decide to remove something from the global object or an object in its prototype chain:

    <script>
    if (randomBoolean())
        delete Object.prototype.hasOwnProperty;
    alert(hasOwnProperty(...));
    </script>

Or you might dynamically decide to interleave assignments and references differently:

    <script>
    function f() { window.undeclared = 42; }
    function g() { alert(undeclared); }
    if (randomBoolean()) {
        f();
        g();
    } else {
        g();
        f();
    }
    </script>

Or you might even dynamically decide to interleave code from separate scripts differently:

    <script>
    setTimeout(function() {
        window.undeclared = 42;
    }, randomInteger());
    </script>
    
    <script>
    setTimeout(function() {
        alert(undeclared);
    }, randomInteger());
    </script>

To be clear: I don't see this as a problem with ES5 strict. These race conditions already always existed, just with more permissive behavior. ES5 strict is trying to complain sooner about likely programming mistakes, which is nice. It's just hopeless in the general case to detect free-variable errors when you're only checking dynamically.

Harmonia designata est,

Dave

On Oct 19, 2010, at 6:21 PM, David Herman wrote:

>> "use strict";
>> var declared = 0;
>> undeclared = declared++;
>> 
>> My understanding is that in ES5 strict mode, declared ends up 1, but in Harmony it ends up 0. In both cases, undeclared is left undefined. Is that correct? Or maybe by "early error" you mean it's a parse error and therefore in Harmony, declared will not be defined at all.
> 
> As Allen says, Harmony is not yet done. But if it truly is lexically scoped (which I hope to be the case) then this program will be rejected at compile time and not executed. Moreover, if part of being lexically scoped means not having the global object in the scope chain (which I hope to be the case), then whether or not a Harmony script *compiles* has no effect on the global object, nor do global variables in Harmony affect the global object.
> 
> As for what happens in ES5 strict, I haven't followed the details of this thread closely enough. Either the check happens before evaluating the RHS, in which case window.declared is 0, or it happens after evaluating the RHS, in which case window.declared is 1.
> 
> Dave
> 
> _______________________________________________
> es5-discuss mailing list
> es5-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es5-discuss



More information about the es5-discuss mailing list