Skip to content

Instantly share code, notes, and snippets.

@gurucharanmk
Last active January 5, 2017 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gurucharanmk/38d8557147d2f542d553b75dfeb40709 to your computer and use it in GitHub Desktop.
Save gurucharanmk/38d8557147d2f542d553b75dfeb40709 to your computer and use it in GitHub Desktop.
var funcArray_1 = [];
var funcArray_2 = [];
//IIFE of type 1
for (var i = 0; i < 10; i++) {
funcArray_1.push(function(val){
return function() { console.log(val); }
}(i));
}
console.log(i); // 10
funcArray_1.forEach(function(func) {
func();
});
//IIFE of type 2
for (var i = 0; i < 10; i++) {
(function(index){
funcArray_2.push(
function(){
console.log(index);
}
);
})(i);
}
console.log(i); // 10
funcArray_2.forEach(function(func) {
func();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment