Created
September 10, 2011 23:13
-
-
Save pragmaticobjects/1208933 to your computer and use it in GitHub Desktop.
Closure iterator
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
| var myarr = ["a", "b", "c"]; | |
| function iterator(arr) { | |
| var idx = 0; | |
| return function () { | |
| return arr[idx++]; | |
| } | |
| } | |
| var getNext = iterator(myarr); | |
| getNext(); // a | |
| getNext(); // b | |
| getNext(); // c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment