Last active
December 22, 2015 08:18
-
-
Save olov/6443605 to your computer and use it in GitHub Desktop.
funny little ES6 for (let;;) example
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
// according to my guesses of what is to come in future ES6 specs: | |
// the loop should execute five times so the program should print five numbers at the end | |
// the fresh-per-iteration x is captured in a closure (that will increment it by 3), | |
// then executed (for side-effects only) in the cond-expr and post-expr. | |
// it will never ever modify the outer x thus five loop iterations | |
(function() { | |
let arr = []; | |
let fn = null; | |
let cnt = 0; | |
for (let x = 0; (fn && fn()), x < 5; (fn && fn()), x++) { | |
console.log("loop iteration " + ++cnt); | |
fn = function() { console.log("incrementing x with value " + x); x += 3 } | |
arr.push(function() { | |
console.log(x); | |
}); | |
} | |
arr.forEach(function(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
// how many iterations? | |
// arr printout? | |
(function() { | |
let arr = []; | |
let fn = null; | |
for (let x = 0; (fn && fn()), x < 5; (fn && fn()), x++) { | |
console.log("loop iteration"); | |
fn = function() { x += 1; } | |
arr.push(function() { | |
console.log(x); | |
}); | |
} | |
arr.forEach(function(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
(function() { | |
let arr = []; | |
let cnt = 0; | |
let fn = null; | |
for (let x = 0; (fn = function() { console.log("incrementing x with value " + x); x += 3 }), x < 5; (fn && fn()), x++) { | |
console.log("loop iteration " + ++cnt); | |
arr.push(function() { | |
console.log(x); | |
}); | |
} | |
arr.forEach(function(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
// this should terminate! | |
(function() { | |
let fn = null; | |
for (let x = 0; ((!fn && (fn = function() { ++x }))), x < 2; fn()) { | |
} | |
})(); |
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
// how many iterations? | |
// arr printout? | |
(function() { | |
let arr = []; | |
let fn = null; | |
for (let x = 0; x < 5; x++) { | |
console.log("loop iteration"); | |
fn = function() { x += 1; } | |
fn(); fn(); | |
arr.push(function() { | |
console.log(x); | |
}); | |
} | |
arr.forEach(function(f) { f(); }); | |
})(); |
yoyoma.js
The log:
option 1
loop iteration
loop iteration
3
6
option 2
loop iteration
loop iteration
loop iteration
loop iteration
loop iteration
2
3
4
5
6
get rid of that 7 and I agree with you (option 2). :)
Just noticed and removed it! Bad head math =/. But yeah, it makes sense.
linking to https://gist.github.com/olov/6443290 before nap
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alternate.js
"loop iteration 1"
"incrementing x with value 0"
"incrementing x with value 3"
"loop iteration 2"
"incrementing x with value 4"
"incrementing x with value 7"
"4"
"7"