Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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