Why are global variables *non-deletable* properties of the global object?
Brendan Eich
brendan at mozilla.org
Thu Jun 19 18:33:49 PDT 2008
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.
> I prefer FF and Safari's behavior to the ES3 specified behavior.
Why? Lower integrity is not your style.
Using var in closures for object state has higher integrity than
using plain old properties. This makes closures better in addition to
the name-hiding (private variable) benefits.
> Which
> should ES3.1 and ES4 codify as correct?
I don't believe you've tested what you think you tested. If you are
using the squarefree.com then you're not testing an ES-anything-
conformant global object implementation!
> 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).
/be
More information about the Es4-discuss
mailing list