The legacy Iterator protocol

Non-standard. 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 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.

Difference between legacy and ES2015 iterator protocols

  • The value was returned directly as a return value of calls to next, instead of the value property of a placeholder object
  • Iteration termination was expressed by throwing a StopIteration object.

Simple example with the old protocol

function makeIterator(array){
    var nextIndex = 0;

    return {
       next: function(){
           if(nextIndex < array.length){
               return array[nextIndex++];
               throw new StopIteration();

var it = makeIterator(['yo', 'ya']);

console.log(; // 'yo'
console.log(; // 'ya'
    if(e instanceof StopIteration){
         // iteration over

See also


© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.

JavaScript Legacy Iterator