Variable declarations inside with statements
On 2008-07-07, at 17:01 EDT, Brendan Eich wrote:
<script> var x = 42; var o = {x:33}; with (o) var x = 21; alert('var x is ' + x + ', o.x is ' + o.x); </script>
alerts "var x is 42, o.x is 21" in Firefox, Opera, and Safari.
Nice to see so many implementations followed the spec., but... ouch!
I bumped my head on that silly var
.
On Jul 7, 2008, at 2:29 PM, P T Withington wrote:
On 2008-07-07, at 17:01 EDT, Brendan Eich wrote:
<script> var x = 42; var o = {x:33}; with (o) var x = 21; alert('var x is ' + x + ', o.x is ' + o.x); </script>
alerts "var x is 42, o.x is 21" in Firefox, Opera, and Safari.
Nice to see so many implementations followed the spec., but... ouch! I bumped my head on that silly
var
.
Yes, it's silly, or worse.
The original Netscape implementation, IIRC (and I should, but I'm
getting old and recycling RAM all the time), did not hoist var in
global code, only in function code. This optimization was cloned in
IE JScript and extended to work in global code too. That change fed
into ECMA-262 Edition 1, since we all agreed after some gnashing of
teeth that it was better to hoist always than only sometimes. (The
Borland guy wanted var to introduce a fresh binding at runtime, only
if evaluated -- that's clearly the wrong answer, and it had no
support in the committee. :-/)
That's hoisting. The interaction with 'with' was a problem in
Netscape 2 for function code, and can be laid at the feet of 'with'
as much as blamed on hoisting.
The whole "ban with from strict mode" idea is plausible if with
causes bugs and confusions in the wild, in any measurable way -- and
if 'with' is otherwise used so little or so trivially that the
migration tax exacted by strict mode can be paid. This is one of the
bones of contention between different people working on ES3.1 and ES4.
Another bone of contention, from what I can see, is about the proper
role of a standard specification in either steering people clear of
trouble, or to take the other side, in trying to teach pigs to sing.
On Jul 7, 2008, at 10:40 AM, Allen Wirfs-Brock wrote:
s/hosted/hoisted/g
There's no "follow[ing] JScript lead" here. The first JavaScript
implementation in Netscape 2, and others followed it. The following
page:
<script> var x = 42; var o = {x:33}; with (o) var x = 21; alert('var x is ' + x + ', o.x is ' + o.x); </script>
alerts "var x is 42, o.x is 21" in Firefox, Opera, and Safari.
That shows poor reasoning. If 'with' in the wild never contains var
declarations, but remains excessively popular, then the argument
fails. But there is no argument from evidence here, AFAICT, only from
aesthetic and best-practices ideas that may not be shared enough to
be worth trying to legislate from the standard.
Also, please stop splitting strict mode preemptively. Let's argue
about what strict mode should forbid, and why, sooner rather than
later. Adding more modes won't get agreement by default. Cc'ing es4- discuss at mozilla.org.