Last active
April 27, 2021 20:26
-
-
Save MoritzKn/8498111820a02ba43cd6731d5106bf1d to your computer and use it in GitHub Desktop.
Demo of generators and iterator
This file contains hidden or 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