3 ms·
Note: this is fixed in ES6. Using `let` instead of `var` will treat each iteration's variable as distinct: a = [] for (let i = 0; i <3; i++) a.
by Strilanc 10y ago
Note: this is fixed in ES6. Using `let` instead of `var` will treat each iteration's variable as distinct:
a = []
for (let i = 0; i <3; i++)
a.push(() => console.log('hi ' + i))
for (let e of a)
e()
Logs:
hi 0
hi 1
hi 2
Actually, it's almost surprising in the opposite direction now.