Skip to content

Instantly share code, notes, and snippets.

@pragmaticobjects
Created September 10, 2011 23:13
Show Gist options
  • Select an option

  • Save pragmaticobjects/1208933 to your computer and use it in GitHub Desktop.

Select an option

Save pragmaticobjects/1208933 to your computer and use it in GitHub Desktop.
Closure iterator
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