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

Re: variable declarations shadowing named properties on window

From: Ojan Vafai <ojan@chromium.org>
Date: Wed, 4 Jan 2012 14:53:11 -0800
Message-ID: <CANMdWTun2CXHcjeuJkAyqs9NzbeZtKpu5bxX1aL_6+qK8mbDUA@mail.gmail.com>
To: Boris Zbarsky <bzbarsky@mit.edu>
Cc: Travis Leithead <travis.leithead@microsoft.com>, Cameron McCormack <cam@mcc.id.au>, "public-script-coord@w3.org" <public-script-coord@w3.org>, Allen Wirfs-Brock <allen@wirfs-brock.com>, Ian Hickson <ian@hixie.ch>
On Wed, Jan 4, 2012 at 10:41 AM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
>
> var declarations don't do any value assignment, per spec.  They just
> define a property if one isn't there already.  Try this testcase:
>
>  <script>
>    x = 5;
>
>  </script>
>  <script>
>    var x;
>    alert(x);
>  </script>
>

TL;DR: It's fine for "var x" to shadow ids/names, but not override existing
global variables.

I know this isn't what you were getting at, but I just want to point out
that this case is different for web developers in terms of writing
maintainable code. Keeping just their scripts from having naming conflicts
is much easier than keeping their scripts and their DOM ids/names from
having naming conflicts.

For example, if you have a minifier for your JS and a minifier for DOM
ids/names they now need to either communicate with each other in some way
or prefix (which negates some of the benefits of minimization).

I expect that for web compat "var x" needs to not override existing global
variables.

Ojan
Received on Wednesday, 4 January 2012 22:53:59 UTC

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