Created
September 25, 2011 04:49
-
-
Save israelst/1240246 to your computer and use it in GitHub Desktop.
An example of how to maintain state with closures
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 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