Re: variable declarations shadowing named properties on window

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