Variable declarations inside with statements

Brendan Eich brendan at
Mon Jul 7 14:44:01 PDT 2008

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.


More information about the Es4-discuss mailing list