Created
October 24, 2011 15:14
-
-
Save roundcorners/1309285 to your computer and use it in GitHub Desktop.
The Iterator!
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
// 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