let and const in loop heads

31 views
Skip to first unread message

Ivan Yan

unread,
Jul 13, 2015, 9:32:52 AM7/13/15
to explor...@googlegroups.com
9.5.1 for loop

const works like var, but you can't change the initial value of a const-declared variable.

I think here should be 'const works like let'.

9.5.2 for-of loop and for-in loop

let arr = [];
for (let i of [0, 1, 2]) {
    arr
.push(() => i);
}
arr
.map(x => x()); // [0,1,2]

In latest firefox developer edition, the result is [2, 2, 2]. 

in the latest Chrome dev, the result is expected:

(function () {
 
'use strict'


  let arr
= []
 
for (let i of [0, 1, 2]) {
    arr
.push(function () {
     
return i
   
})
 
}


  let r
= arr.map(function (x) {
   
return x()
 
})


  console
.log(r) // [0, 1, 2]
}())



Reply all
Reply to author
Forward
0 new messages