Closure in Javascript [duplicate]

Although the (correct) answer is the duplicate, I’d like to point out a more advanced method – replacing loops with iterators, which effectively solves javascript “late-bound” closures problem.

loop = function(start, end, step, body) {
    for(var i = start; i != end; i += step)
       body(i)
}

loop(1, 100, 1, function(i) {
   // your binding stuff
})

Leave a Comment