Demo of generators and iterator
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
let a = [1, 2, 3, 4]; | |
for (let item of a) { | |
console.log(item); | |
} | |
function* reverse () { | |
for (let i = a.length - 1; i >= 0; i--) { | |
yield a[i]; | |
} | |
} | |
a[Symbol.iterator] = reverse; | |
for (let item of a) { | |
console.log(item); | |
} | |
console.log([...a]); | |
console.log(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment