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 1056610 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: 1056610
  • Created:
  • Creator: cughudson_1
  • Is current revision? No
  • Comment

Revision Content

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

Property Value
next A zero arguments function that returns an value.

传统迭代协议与ES2015中迭代协议的异同D

  • 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>未标准化.</strong>&nbsp;传统迭代协议是Mozilla javaScript 引擎SpiderMonkey所特有的功能,该功能在未来可能会被移除,在使用时,请使用for..of循环和迭代协议替换该功能。</div>

<h2 id="The_deprecated_Firefox-only_iterator_protocol">被遗弃的Firefox所特有的迭代协议</h2>

<p>在Firefox浏览器的第26个版本中,已经实现了另外一个类似于<a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">ES2015 Iterator protocol</a>,的迭代协议。</p>

<p>当一个对象正在调用next()方法时,这时该对象就是一个传统迭代,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_ES2015_iterator_protocols">传统迭代协议与ES2015中迭代协议的异同D</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