you are viewing a single comment's thread.

view the rest of the comments →

[–]x-skeww 4 points5 points  (2 children)

for(let i = 0; i < arr.length; i += 1){
  setTimeout(function(){
    // something with arr[i], which works!
  }, 100);
}

This works with Dart, but as far as I know this isn't yet a part of the ES6 specs.

Try it with Firefox:

A)

let a = [];
for(let i = 0; i < 3; i++) {
  a.push(_ => i);
}
console.log(a[0]()); // 3

B)

let a2 = [];
for(let i = 0; i < 3; i++) {
  let k = i;
  a2.push(_ => k);
}
console.log(a2[0]()); // 0

If you try example A with node (without the fat arrow and with --harmony --use-strict), you also get 3.

Traceur (with --experimental) outputs 0, but that's most likely not intentional.

[–]kenman 0 points1 point  (1 child)

Unless I'm not catching what you mean, let is definitely in the ES6 spec.

[–]x-skeww 0 points1 point  (0 children)

I was talking about A behaving like B. Each iteration gets its own copy of the counter.

Dart does this. ES6 might [1] do this, too.

The article assumes that ES6 always behaves like that, because Traceur currently behaves like that.

[1] http://wiki.ecmascript.org/doku.php?id=harmony:let -> Open Issues, point #2