Skip to content

Instantly share code, notes, and snippets.

@israelst
Created September 25, 2011 04:49
Show Gist options
  • Save israelst/1240246 to your computer and use it in GitHub Desktop.
Save israelst/1240246 to your computer and use it in GitHub Desktop.
An example of how to maintain state with closures
function create_iterator(list){
var index = 0;
return function(){
if(index == list.length){
return false;
}else{
return list[index++];
}
}
}
var list = [1, 1, 2, 3, 5, 8, 13, 21];
var iterator = create_iterator(list);
while(value = iterator()){
console.log(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment