Skip to content

Instantly share code, notes, and snippets.

@novemberborn
Created December 16, 2010 10:48
Show Gist options
  • Save novemberborn/743278 to your computer and use it in GitHub Desktop.
Save novemberborn/743278 to your computer and use it in GitHub Desktop.
Closures inside for-loops
var results = [];
for(var i = 0; i < 10; i++){
// Bad:
// results.push(function(i){ return i; });
// 'Good':
results.push((function(i){
return function(){ return i; };
})(i));
}
console.log(results.map(function(f){ return f(); }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment