On 2010-06-07, at 1:22 PM, chevalma@iro.umontreal.ca wrote:
For example, is the program
var foo = 1; print(foo);
equivalent to
{ var foo = 1; print(foo); }
?
It is hard to decipher the ECMAScript spec to guarantee that it is so (which I think it is). Anyone have an opinion?
I think that ECMAScript has only one scope per function, and so your transfomation is valid, however, you'll have to test this.
It may be different because the foo variable was previously a global in the scope external to the block, while now it will be a local of the function, and undefined until the initialization is reached.
In my example I'm comparing *programs*, i.e.
1) a sequence of 2 statements at top level 2) a single block at top level which contains 2 statements
There are no function definitions in this code. Note that in v8 the program
{ var foo = 1; print(foo); } print(foo);
prints 1 twice. So foo is defined as a global variable.
Marc