Created
August 20, 2015 11:14
-
-
Save jainrocks/c8febb43b253bb3b46f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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