Last active
October 20, 2015 03:11
-
-
Save taka011239/1d2c8a811969953e540d to your computer and use it in GitHub Desktop.
letのblock scop
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
for (var i = 0; i < 5; i++) { | |
setTimeout(function () { | |
console.log(i); | |
}, i * 1000); | |
} | |
// 5, 5, 5, 5, 5 |
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
for (let i = 0; i < 5; i++) { | |
setTimeout(function () { | |
console.log(i); | |
}, i * 1000); | |
} | |
// 0, 1, 2, 3, 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ループカウンタに
let
を使うと、iteration毎にiが宣言され、直前のiteration後の値で初期化される。for
ブロック内でループカウンタをrebind、closure
に引数でループカウンタを渡すことは不要。