Skip to content

Instantly share code, notes, and snippets.

@josephdicdican
Forked from btg5679/simpleIterator.js
Created July 20, 2018 02:41
Show Gist options
  • Save josephdicdican/5c001beb4e085eeaaf044844b6f4f108 to your computer and use it in GitHub Desktop.
Save josephdicdican/5c001beb4e085eeaaf044844b6f4f108 to your computer and use it in GitHub Desktop.
Simple Iterator
function makeIterator(array) {
var nextIndex = 0;
console.log("nextIndex =>", nextIndex);
return {
next: function() {
return nextIndex < array.length
? { value: array[nextIndex++], done: false }
: { done: true };
}
};
}
var it = makeIterator(["simple", "iterator"]);
console.log(it.next()); // {value: 'simple, done: false}
console.log(it.next()); // {value: 'iterator, done: false}
console.log(it.next()); // {done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment