{{jsSidebar("More")}}
未标准化. 传统迭代协议是Mozilla javaScript 引擎SpiderMonkey所特有的功能,该功能在未来可能会被移除,在使用时,请使用for..of循环和迭代协议替换该功能。
被遗弃的Firefox所特有的迭代协议
在Firefox浏览器的第26个版本中,已经实现了另外一个类似于ES2015 Iterator protocol,的迭代协议。
当一个对象正在调用next()方法时,这时该对象就是一个传统迭代,An object is an legacy iterator when it implements a next()
method with the following semantics, and throws {{jsxref("Global_Objects/StopIteration", "StopIteration")}} at the end of iteration.
属性 | 取值 |
---|---|
next |
. |
传统迭代协议与ES2015中迭代协议的异同D
- The value was returned directly as a return value of calls to
next
, instead of thevalue
property of a placeholder object - Iteration termination was expressed by throwing a {{jsxref("Global_Objects/StopIteration", "StopIteration")}} object.
Simple example with the old protocol
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 } }