Firefox在version 26版本生效的与ES2015迭代协议相似的另一种迭代协议。以下统称为旧迭代协议。
?旧迭代器对象在使用next()方法时,会在循环到最后时抛出StopIteration。
| 属性 | ??描述 |
|---|---|
next |
一个会返回值的无参数函数 |
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
}
}