HTML and CSS Reference
In-Depth Information
var iterator = tddjs.iterator(collection);
assertSame(collection[0], iterator.next());
assertTrue(iterator.hasNext());
},
"test hasNext should be false after last item":
function () {
var collection = [1, 2];
var iterator = tddjs.iterator(collection);
iterator.next();
iterator.next();
assertFalse(iterator.hasNext());
},
"test should loop collection with iterator":
function () {
var collection = [1, 2, 3, 4, 5];
var it = tddjs.iterator(collection);
var result = [];
while (it.hasNext()) {
result.push(it.next());
}
assertEquals(collection, result);
}
});
A possible implementation of the iterator is shown in Listing 6.22.
Listing 6.22 Possible implementation of tddjs.iterator
(function () {
function iterator(collection) {
var index = 0;
var length = collection.length;
function next() {
var item = collection[index++];
return item;
}
function hasNext() {
 
Search WWH ::




Custom Search