The global object as the "global scope instance object"

Andreas Rossberg rossberg at google.com
Tue Jan 24 02:37:59 PST 2012


On 23 January 2012 19:25, Brendan Eich <brendan at mozilla.org> wrote:
>Andreas Rossberg<mailto:rossberg at google.com>:
>> V8 currently allows
>>
>>   var w = 1; w = 2; print(w); const w = 3
>>
>> which will output 2. The idea most likely was that const should behave
>> like var. This, and other, similar examples clearly have to break if
>> should const become official in classic mode, so the compatibility
>> argument may not carry far.
>
> SpiderMonkey:
>
> js> var w = 1; w = 2; print(w); const w = 3
> typein:19: TypeError: redeclaration of var w:
> typein:19: var w = 1; w = 2; print(w); const w = 3
> typein:19: ....................................^
>
> js> var w = 1
> js> const w = 3
> typein:22: TypeError: redeclaration of var w

However:

js> const w = 3; var w = 1; print(w);
3

> Why does V8 do something else, do you know the impetus for diverging?

No, unfortunately I don't know the full history there. I can try to find it out.

But just to be obnoxious, with Firefox:

print(c);
Object.defineProperty(this, 'c', {value: 1});
print(c);
const c = 2;
print(c);
=> undefined 1 2

Or even:

print(c);
Object.defineProperty(this, 'c', {value: 1, writable: true});
print(c);
c = 2;
print(c);
const c = 3;
print(c);
=> undefined 1 2 3

Apparently, FF avoids breaking the object model only for the price of
keeping the non-writable const property configurable until the actual
initialization -- effectively breaking const completely.

If the property wasn't configurable, then the initialization would
need to modify a non-writable, non-configurable data property, which
is a clear violation of the JS object model. It is what V8 does,
though. So in V8, the above examples are rejected, but instead,
objects break.

Consequently, I maintain my claim that it is impossible to reconcile
sane let/const semantics with the idea of having toplevel bindings
represented as data properties on the global object. If there is some
trick for "hiding" accessor semantics that doesn't break the object
model then I'd be really interested in seeing it. :)

/Andreas


More information about the es-discuss mailing list