This very good Stack Overflow answer gives an example of how closures can work counter intuitively when returning anonymous functions within a list.
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + i;
result.push( function() {alert(item + ' ' + list[i])} );
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
// Using j only to help prevent confusion -- could use i.
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
This code will return:
item2 undefined
item2 undefined
item2 undefined
My question is - how would you modify this code so that it returns the result we're expecting?
item0 1
item1 2
item2 3