Skip to content

Instantly share code, notes, and snippets.

@jainrocks
Created August 20, 2015 11:14
Show Gist options
  • Save jainrocks/c8febb43b253bb3b46f7 to your computer and use it in GitHub Desktop.
Save jainrocks/c8febb43b253bb3b46f7 to your computer and use it in GitHub Desktop.
// JS closures
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + list[i];
//alert(item + ' ' + list[i]);
//result.push(function() { alert(item + ' ' + list[i]) });
result.push(Foo(item + ' ' + list[i]));
}
return result;
}
function testList() {
var fnList = buildList([1, 2, 32]);
for (var j = 0; j < fnList.length; j++) {
fnList[j]();
}
}
function Foo(item){
return function() { alert(item); }
}
testList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment