Created
August 8, 2014 20:17
-
-
Save yanatan16/e92a864979e961a14346 to your computer and use it in GitHub Desktop.
Where js's for-loop var leaking is bad
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
var arr = [1,2,3,4]; | |
for (var i = 0, len = arr.length; i < len; i++) { | |
var el = arr[i]; | |
setTimeout(function () { | |
console.log(el); | |
}, 10); | |
} | |
// output: | |
// 4 | |
// 4 | |
// 4 | |
// 4 |
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
var arr = [1,2,3,4]; | |
arr.map(function (el) { | |
setTimeout(function () { | |
console.log(el); | |
}, 10) | |
}) | |
// output: | |
// 1 | |
// 2 | |
// 3 | |
// 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment