Skip to content

Instantly share code, notes, and snippets.

@raduchiriac
Last active March 10, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raduchiriac/807d4e80c3ae88ef8f41 to your computer and use it in GitHub Desktop.
Save raduchiriac/807d4e80c3ae88ef8f41 to your computer and use it in GitHub Desktop.
Understandable Closures
function buildFun(n){
var res = [];
for (var i = 0; i< n; i++){
(function(number){
res.push(function(){
return number;
});
})(i)
}
return res;
}
// Craziness
return [...Array(n)].map((_, i) => () => i);
// ES6 (just beautiful)
res.push((i => () => i)(i));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment