Created
May 25, 2015 10:21
-
-
Save kishmiryan-karlen/3b6f3f80edc49878a335 to your computer and use it in GitHub Desktop.
Simple JS 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 createIterator(items) { | |
var currentIndex = 0; | |
return { | |
next: function() { | |
if (items[currentIndex]) { | |
return { value: items[currentIndex], done: false }; | |
} else { | |
return { value: undefined, done: true }; | |
} | |
currentIndex++; | |
} | |
} | |
} | |
// Usage | |
var iter = createIterator([1, 2, 3]); | |
console.log(iter.next()); | |
console.log(iter.next()); | |
console.log(iter.next()); | |
console.log(iter.next()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment