lexical for-in/for-of loose end

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Jan 30 18:09:07 PST 2012


Here is valid ES6 for-in statement:

for (let p=alert("initializing p"+p) in  [0,1]) alert(p);

Each iteration gets a fresh p, but does it produce three alerts saying:
  Initializing p
  0
  1
or four alerts saying
  Initializing p
  0
  Initializing p
  1

I would expect the first alternative.

However, taking the first alternative, what happens for

let p="outer";
for (let p=alert("initializing p"+p) in  [0,1]) alert(p);

Is the first alert: initializing outer
or does it throw a reference error for a temporal dead zone violation?

I would expect the reference error. 

Essentially the for-in is conceptually wrapped with a block that has an uninitialized binding for p.  The initializer of the let declaration is evaluated once in the scope of that block and then the value is discarded because it is inaccessible.  For each iterations, a fresh block is created containing a binding for p that is initialized to the current iteration key and then the statement is evaluated in the scope of that block.

Thoughts?

Allen




More information about the es-discuss mailing list