Skip to content

Instantly share code, notes, and snippets.

@MoritzKn
Last active April 27, 2021 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoritzKn/8498111820a02ba43cd6731d5106bf1d to your computer and use it in GitHub Desktop.
Save MoritzKn/8498111820a02ba43cd6731d5106bf1d to your computer and use it in GitHub Desktop.
Demo of generators and iterator
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