I would like to do the something along the following:
for (var i = 0; i < 10; ++i) {
createButton(x, y, function() { alert("button " + i + " pressed"); }
}
The problem with this is that I always get the final value of i because Javascript’s closure is not by-value.
So how can I do this with javascript?
Note that JSLint doesn’t like this pattern. It throws “Don’t make functions within a loop.”.
Live demo: http://jsfiddle.net/simevidas/ZKeXX/