Created
October 13, 2016 00:14
-
-
Save Sysetup/8e24962836e2b69645b0ec00040c4ca7 to your computer and use it in GitHub Desktop.
Iterators basic example.
This file contains hidden or 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(){ | |
| var nextIndex = 0; | |
| return { | |
| next: function(){ | |
| return nextIndex < 5 ? { value: nextIndex++, done: false} : {done: true} | |
| } | |
| } | |
| } | |
| var it = makeIterator(); | |
| console.log('Value: '+ it.next().value); | |
| console.log('Value: '+ it.next().value); | |
| console.log('Done: ' + it.next().done); | |
| console.log('Value: '+ it.next().value); | |
| console.log('Value: '+ it.next().value); | |
| console.log('Value: '+ it.next().value); | |
| console.log('Done: ' + it.next().done); | |
| //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generators |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment