lexical for-in/for-of loose end
rossberg at google.com
Fri Feb 3 02:15:41 PST 2012
On 3 February 2012 03:08, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> Plus, the desugarings aren't things that are really suitable for teaching
> the semantics to everyday JS programmers. You instead have to say something
> Ok, this is really complicated but here goes. For each let/const declared
> in the for header, a fresh variable is located in the loop body for each
> iteration of the loop. However, the values of the loop variables are
> automatically copied from the previous iteration into the next iterations.
> This means that basic expression operator will work in the loop header
> pretty much like you would expect. But be careful if you use any function
> expressions in the for header because the loop variables they reference may
> not be from the current iteration and any changes to loop variable they make
> may not have the effect you intended. But, hey you probably shouldn't do
> those things so it really doesn't matter what it really does.
I don't understand your bit saying that values are "copied from the
previous iteration into the next iterations". In any case, it seems to
me that saying the following is perfectly enough:
"Declarations in the loop header work like normal declarations.
However, inside the loop body, for each iteration, you get a fresh
copy of all variables bound in the header, with their current values.
(This does not affect uses of the variables in the loop header
The last sentence is just for clarity, as it is already implied by the
other two. Moreover, it shouldn't matter for any sane kind of program.
More information about the es-discuss