Skip to content

Instantly share code, notes, and snippets.

@j2doll
Last active November 22, 2018 04:57
closure
// closure.js
// var
var funcs = [];
for(var i = 0; i < 10; i += 1) {
funcs.push( function(){return i;} );
}
console.log('i : ' + i); // 10
funcs.forEach(function(func){
func(); // 10 * ten times
});
// let
let funcs2 = [];
for(let j = 0; j < 10; j += 1) {
funcs2.push( function(){return j;} );
}
funcs2.forEach(function(func2){
func2(); // 0, 1, 2, 3, 4, ..., 9
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment