Skip to content

Instantly share code, notes, and snippets.

@behnamazimi
Created December 27, 2019 11:23
Show Gist options
  • Save behnamazimi/d7b73237abdea4e48bc00aa7f2c7f609 to your computer and use it in GitHub Desktop.
Save behnamazimi/d7b73237abdea4e48bc00aa7f2c7f609 to your computer and use it in GitHub Desktop.
const data = {
items: ["A", "B", "C"],
pointerIndex: 0,
[Symbol.iterator]: function () {
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 };
}
}
const it = data[Symbol.iterator];
console.log(it()) //o: {value: "A", done: false}
console.log(it()) //o: {value: "B", done: false}
console.log(it()) //o: {value: "C", done: false}
console.log(it()) //o: {value: undefined, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment