Skip to content

Instantly share code, notes, and snippets.

@behnamazimi
Created December 27, 2019 11:23
Show Gist options
  • Save behnamazimi/f34c3dc0110200c257f612338cbf8d37 to your computer and use it in GitHub Desktop.
Save behnamazimi/f34c3dc0110200c257f612338cbf8d37 to your computer and use it in GitHub Desktop.
const data = {
items: ["A", "B", "C"],
pointerIndex: 0,
next() {
if (this.pointerIndex < this.items.length) // if done
return { value: this.items[this.pointerIndex++], done: false }
else // if not done
return { value: undefined, done: true };
},
[Symbol.iterator]: function() {
this.pointerIndex = 0;
return { next: this.next.bind(this) }
}
}
for (let item of data) {
console.log(item)
} //o: A B C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment