Skip to content

Instantly share code, notes, and snippets.

@gurucharanmk gurucharanmk/iife.js

Last active Jan 5, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.