Firefox, prior to version 26 implemented another iterator protocol that is similar to the standard ES2015 Iterator protocol .
An object is an legacy iterator when it implements a
next()
method with the following semantics, and throws
StopIteration
at the end of iteration.
| Property | Value |
|---|---|
next
|
A zero arguments function that returns an value. |
next
, instead of the
value
property of a placeholder object
StopIteration
对象。
function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
if(nextIndex < array.length){
return array[nextIndex++];
else
throw new StopIteration();
}
}
}
var it = makeIterator(['yo', 'ya']);
console.log(it.next()); // 'yo'
console.log(it.next()); // 'ya'
try{
console.log(it.next());
}
catch(e){
if(e instanceof StopIteration){
// iteration over
}
}