The legacy iterator protocol is a SpiderMonkey-specific feature, and will be removed at some point. For future-facing usages, consider using for..of loops and the iterator protocol
The deprecated Firefox-only iterator protocol
Firefox, prior to version 26 implemented another iterator protocol than {{jsxref("Guide/The_Iterator_protocol", "ES6 iterator protocol")}}.
An object is an legacy iterator when it implements a next()
method with following semantics, and throws {{jsxref("Global_Objects/StopIteration", "StopIteration")}} on the end of iteration.
Property | Value |
---|---|
next |
A zero arguments function that returns an value. |
Difference between legacy and ES6 iterator protocols
- 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
Example
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 } }