Re: variable declarations shadowing named properties on window

Travis Leithead:
>> I thought Cameron proposed another alternative, which I much prefer
>> over adding another object to the prototype chain:
>>> We could either make [[HasProperty]] return false for named properties
>>> on window despite [[GetOwnProperty]] returning a property descriptor.
>>> Or, we could make the window object have a custom [[GetProperty]]
>>> instead of [[GetOwnProperty]], so that [[HasProperty]] returns false and
>>> [[GetOwnProperty]] returns undefined, while fetching the property off
>>> the object still gives you the named property value.

Boris Zbarsky:
> It's not entirely clear to me whether this works for the window in
> particular, because as far as I can tell the bareword lookup algorithm
> in ES relies on [[HasProperty]] before ever calling [[Get]]. I could
> well be wrong on that, though; I don't follow that section of the spec
> very well.

Right, I don't think either of my proposals Travis quoted will work now, 
due to this bareword lookup problem.  Here is my reading of the ES5 spec 
for barewords:

   Bare identifiers are evaluated here:

   which does Identifier Resolution:

   which calls GetIdentifierReference:

   where the lexical environment passed to it is the unique "global

   whose Environment Record is an object environment record.

   GetIdentifierReference calls HasBinding on its Environment
   Record, which because it is an object environment record is
   this operation:

   which calls [[HasProperty]], which will return false for named
   properties if we define it so.

I think the upshot of this is that with named properties exposed on this 
window object itself, there is no way to have

   1. var statements with no assignment shadow the named property
      with "undefined", and

   2. allow bareword identifiers to reference named properties

because if [[HasProperty]] returns true, (1) will fail but (2) will 
work, and if it returns false, (1) will work but (2) will fail.

Received on Thursday, 5 January 2012 22:45:57 UTC