Let's say I have this code
var callbacks = {};
for (var i in mylist){
callbacks[i] = { callback: function(){ myFunction( myList[i].someProperty ); }};
}
The code above would cause on a list like this:
myList[0].property = "zero";
myList[1].property = "one";
a result like this:
callbacks[0].callback // -> returns myFunction("one"); WRONG!
callbacks[1].callback // -> returns myFunction("one");
Is there anything I can do (in the for..in
loop and without changing the fact that I access myList[i].someProperty
inside of an anonymous function) to make sure that the value of myList[i].someProperty
is assigned during the loop and not when the function is executed?