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

Re: variable declarations shadowing named properties on window

From: Andreas Rossberg <rossberg@google.com>
Date: Thu, 5 Jan 2012 13:31:32 +0100
Message-ID: <CADjR9Nox6J1=kdyomDFbDhVfbZvUq6+jPhyHQnqmHR5pFGmRbQ@mail.gmail.com>
To: public-script-coord@w3.org
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 14:34:35 UTC

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