5 ms·
I think this was discussed there: https://news.ycombinator.com/item?id=8933979 https://news.ycombinator.com/item?id=8933979
by garaetjjte 5y ago
I think this was discussed there: https://news.ycombinator.com/item?id=8933979 https://news.ycombinator.com/item?id=8933979
- mercora 5y agothis did indeed help understanding and fixing the issue.
- davnicwil 5y agoJust in case this is interesting, a potentially more elegant way to solve this with modern JS is to use 'for(let i' instead of 'for(var i'. The reason is that 'let' gives 'i' 'block scope', meaning that each iteration of the loop block gets its own scope so that 'i' isn't overwritten each time, and each function you declare captures the value of 'i' at the time it was declared. In other words - the way you originally expected 'var' to behave is how 'let' behaves!