Why are global variables *non-deletable* properties of the global object?

Maciej Stachowiak mjs at apple.com
Thu Jun 19 18:55:40 PDT 2008


On Jun 19, 2008, at 6:33 PM, Brendan Eich wrote:

> On Jun 19, 2008, at 4:39 PM, Mark S. Miller wrote:
>
>> In ES3:
>>
>> 10.2.1	Global Code
>> The scope chain is created and initialised to contain the global
>> object and no others.
>> Variable instantiation is performed using the global object as the
>> variable object and using property attributes { DontDelete }.
>> The this value is the global object.
>>
>> I was puzzled by the DontDelete in the above spec language. Firefox
>> and Safari seems to disagree:
>>
>>
>>
>> 'foo' in window
>> false
>>
>> var foo = 3;
>>
>> 'foo' in window
>> true
>>
>> delete window.foo
>> true
>>
>> 'foo' in window
>> false
>>
>
> How did you test? Try putting this in a Firefox address toolbar:
>
> javascript:alert('foo' in window); var foo = 42; alert(delete foo);
> alert(foo)
>
> You will get true (because the var binding -- not initialization --
> is hoisted to the top of the program, right after the javascript:),
> false (because var makes a DontDelete binding outside of eval), and  
> 42.


Safari will do the same. Global var bindings are most definitely  
DontDelete, and should certainly stay that way, in my opinion.

>
>
>> Should we drop the
>> "DontDelete" in the spec language?
>
> Absolutely not. Besides giving integrity guarantees to programmers,
> it gives implementations (including SpiderMonkey and JavaScriptCore)
> optimization opportunities (binding name to slot ahead of time, or on
> first among many uses).

Strongly agreed.

Regards,
Maciej




More information about the Es4-discuss mailing list