In JavaScript, what is the best way to create anonymous function using the current value of a variable, at the moment the anonymous function is created?
This bogus code for example will use the currentObject value at runtime, when the anonymous function is called. So instead to use objects[i], it uses objects[] (which is absolutely normal).
for (var i = 0 ; i < objects.length ; i++) {
var currentObject = objects[i];
foo.bind(currentObject.quux, function() {
bar(currentObject.baz);
});
}
How best to achieve without an eval() this behavior?