Skip to content

Instantly share code, notes, and snippets.

@btg5679
Created July 11, 2018 00:52
Show Gist options
  • Save btg5679/91048cc658db67594b6fe2bca4c70ef1 to your computer and use it in GitHub Desktop.
Save btg5679/91048cc658db67594b6fe2bca4c70ef1 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}
@zachBridges
Copy link

This comment and the one above do not have the delimiting ' i.e. 'simple'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment