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 1056662 of The legacy Iterator protocol

  • Revision slug: Web/JavaScript/Reference/Deprecated_and_obsolete_features/The_legacy_Iterator_protocol
  • Revision title: The legacy Iterator protocol
  • Revision id: 1056662
  • Created:
  • Creator: KadirTopal
  • Is current revision? Yes
  • Comment Revert to revision of 2016-02-21 05:37:52 by fscholz: "accidental translation by user"

Revision Content

{{jsSidebar("More")}}
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 {{jsxref("Global_Objects/StopIteration", "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 {{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
    }
}

See also

Revision Source

<div>{{jsSidebar("More")}}</div>

<div class="warning"><strong>Non-standard.</strong> 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">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 that is similar to the standard <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">ES2015 Iterator protocol</a>.</p>

<p>An object is an legacy iterator when it implements a <code>next()</code> method with the following semantics, and throws {{jsxref("Global_Objects/StopIteration", "StopIteration")}} at 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>A zero arguments function that returns an value.</td>
  </tr>
 </tbody>
</table>

<h3 id="Difference_between_legacy_and_ES6_iterator_protocols">Difference between legacy and ES2015 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>

<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>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators">Iterators and generators</a></li>
 <li>More <a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features">deprecated and obsolete features</a></li>
</ul>
Revert to this revision