-
-
Save josephdicdican/5c001beb4e085eeaaf044844b6f4f108 to your computer and use it in GitHub Desktop.
Simple Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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