Skip to content

Instantly share code, notes, and snippets.

@roundcorners
Created October 24, 2011 15:14
Show Gist options
  • Save roundcorners/1309285 to your computer and use it in GitHub Desktop.
Save roundcorners/1309285 to your computer and use it in GitHub Desktop.
The Iterator!
// Our iterator
var iterator = (function() {
var index = -1,
// Our data
data = ['joe', 'chapman', 'developer', 'UK'],
// cache the length
length = data.length;
return {
next: function() {
if (this.hasNext()) {
index++;
return data[index];
}
return null;
},
hasNext : function() {
return index < length-1;
}
};
}());
var list = document.getElementById('list'),
data = [];
// Testing
while(iterator.hasNext()) {
data.push('<li>' + iterator.next() + '</li>');
}
list.innerHTML = data.join('');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment