Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 706849 of The legacy Iterator protocol

  • Revision slug: Web/JavaScript/Guide/The_legacy_Iterator_protocol
  • Revision title: The legacy Iterator protocol
  • Revision id: 706849
  • Created:
  • Creator: arai
  • Is current revision? No
  • Comment

Revision Content

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 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 the value 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
    }
}

Revision Source

<div class="warning">
 The legacy iterator protocol is a SpiderMonkey-specific feature, and will be removed at some point. For future-facing usages, consider using <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of" title="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for..of</a> loops and the <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">iterator protocol</a></div>
<h2 id="The_deprecated_Firefox-only_iterator_protocol">The deprecated Firefox-only iterator protocol</h2>
<p>Firefox, prior to version 26 implemented another iterator protocol than <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">ES6 Iterator protocol</a>.</p>
<p>An object is an legacy iterator when it implements a <code>next()</code> method with following semantics, and throws {{jsxref("Global_Objects/StopIteration", "StopIteration")}} on the end of iteration.</p>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Property</th>
   <th scope="col">Value</th>
  </tr>
  <tr>
   <td><code>next</code></td>
   <td>
    <p>A zero arguments function that returns an value.</p>
   </td>
  </tr>
 </tbody>
</table>
<h3 id="Difference_between_legacy_and_ES6_iterator_protocols">Difference between legacy and ES6 iterator protocols</h3>
<ul>
 <li>The value was returned directly as a return value of calls to <code>next</code>, instead of the <code>value</code> property of a placeholder object</li>
 <li>Iteration termination was expressed by throwing a {{jsxref("Global_Objects/StopIteration", "StopIteration")}} object.</li>
</ul>
<h3 id="Simple_example_with_the_old_protocol">Simple example with the old protocol</h3>
<h2 id="Example">Example</h2>
<pre class="brush: js">
function makeIterator(array){
    var nextIndex = 0;

    return {
       next: function(){
           if(nextIndex &lt; 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
    }
}
</pre>
Revert to this revision