W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2012

Re: variable declarations shadowing named properties on window

From: Brendan Eich <brendan@mozilla.org>
Date: Fri, 6 Jan 2012 08:59:54 -0800
Cc: public-script-coord@w3.org
Message-Id: <F25C5561-CB29-4475-92BB-7791DA58C375@mozilla.org>
To: Andreas Rossberg <rossberg@google.com>
See https://bugs.ecmascript.org/show_bug.cgi?id=78 -- the spec is changing.

/be

On Jan 5, 2012, at 4:31 AM, Andreas Rossberg wrote:

> Unfortunately, inserting an additional object into the prototype chain
> doesn't actually give the desired shadowing semantics.
> 
> Here once more is a breakdown of what the ES5 spec says:
> 
> - For "var x" (whether with a definition or without), invoke
> HasBinding on the current environment. If it returns true, do nothing,
> otherwise create x in that environment. [10.5 8c]
> 
> - For the global scope, the environment is the global environment,
> which is an object environment
> associated with the global object (i.e. window). [10.2.3]
> 
> - For an object environment, HasBinding simply delegates to the
> HasProperty internal method of the associated object. [10.2.1.2.1]
> 
> - HasProperty delegates to GetProperty and returns true if the result
> isn't undefined. [8.12.6]
> 
> - GetProperty walks the prototype chain until the property is found
> via GetOwnProperty, or returns undefined if it isn't found. [8.12.2]
> 
> In other words, "var x" in the top-level scope ought to have no effect
> if x already exists on window _or any of its prototypes_! Likewise,
> "var x = e" simply becomes "x = e" in that case.
> 
> Yes, the semantics of "var" in JavaScript is insane, but unfortunately
> that's what the current spec says. I don't see any ES5-conformant way
> of enabling the kind of shadowing discussed. It may be worth proposing
> a change for ES6, though.
> 
> /Andreas
> 
> 
Received on Friday, 6 January 2012 17:02:58 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:05 UTC