Last active
March 10, 2016 19:44
-
-
Save raduchiriac/807d4e80c3ae88ef8f41 to your computer and use it in GitHub Desktop.
Understandable Closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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