Last active
August 29, 2015 14:17
-
-
Save ludekstepan/8f7799cd8be8600266ee to your computer and use it in GitHub Desktop.
Block scoping of 'let' in for loop
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
"use strict"; | |
var stack = []; | |
for (let i = 0; i < 10; i++) { | |
console.log("inner: " + i); | |
i += 4; | |
stack.push(() => console.log("outer: " + i)); | |
} | |
stack.forEach(f => f()); |
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
inner: 0 | |
inner: 1 | |
inner: 2 | |
inner: 3 | |
inner: 4 | |
inner: 5 | |
inner: 6 | |
inner: 7 | |
inner: 8 | |
inner: 9 | |
outer: 4 | |
outer: 5 | |
outer: 6 | |
outer: 7 | |
outer: 8 | |
outer: 9 | |
outer: 10 | |
outer: 11 | |
outer: 12 | |
outer: 13 |
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
"use strict"; | |
var stack = []; | |
for (let i = 0; i < 10; i++) { | |
console.log("inner: " + i); | |
i += 4; | |
// stack.push(() => console.log("outer: " + i)); | |
} | |
stack.forEach(f => f()); | |
// Output: | |
// inner: 0 | |
// inner: 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
"inner: 0" | |
"inner: 5" | |
"outer: 10" | |
"outer: 10" |
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
node --version | |
v0.12.0 | |
node --harmony for-let.js | |
inner: 0 | |
inner: 5 | |
outer: 4 | |
outer: 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment