Variable declarations inside with statements

# Brendan Eich (17 years ago)

On Jul 7, 2008, at 10:40 AM, Allen Wirfs-Brock wrote:

Herman,

I think you are correct, according to the ES3 standard. The rules
of variable instantiation would host the variable to the scope
level of the function and initially set it to undefined. However,
the algorithm you quoted, if executed inside a with statement
scoped to an object that includes a property with that identifier
name should assign the value to the property, not the hosted variable.

s/hosted/hoisted/g

We need to check out all the common browser implementations to see
what the other ones do and we need to add the results to the our
discrepancies doc. If a majority of browsers follow the JScript
lead in not implementing what the specification says we can change
the spec. in ES3.1 (although, is isn’t clear that we have a good
specification mechanism available for describing the actual
behavior you observe).

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.

Of course, this is just further reinforcement of why the ES 3.1
working group wants eliminate with statements from the ES3.1
“cautious” subset.

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.

# P T Withington (17 years ago)

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.

# Brendan Eich (17 years ago)

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.