Skip to content

Instantly share code, notes, and snippets.

@FiNGAHOLiC
Created January 19, 2012 11:05
Show Gist options
  • Select an option

  • Save FiNGAHOLiC/1639430 to your computer and use it in GitHub Desktop.

Select an option

Save FiNGAHOLiC/1639430 to your computer and use it in GitHub Desktop.
Iterator Pattern
// http://shichuan.github.com/javascript-patterns/
var agg = (function(){
var index = 0;
var data = [1, 2, 3, 4, 5];
var length = data.length;
return {
next : function(){
var element;
if(!this.hasNext()) return null;
element = data[index];
index = index + 2;
return element;
},
hasNext : function(){
return index < length;
},
rewind : function(){
index = 0;
},
current : function(){
return data[index];
}
};
}());
while(agg.hasNext()){
console.log(agg.next());
};
agg.rewind();
console.log(agg.current());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment