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 707025 of Iterator

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Iterator
  • Revision title: Iterator
  • Revision id: 707025
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Objects")}}
Non-standard. The Iterator function 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.

Summary

The Iterator function returns an object which implements legacy iterator protocol and iterates over enumerable properties of an object.

Syntax

Iterator(object)

Parameters

object
Object to iterate over properties.

Description

An overview of the usage is available on the Iterators and Generators page.

Properties

Methods

Iterator.prototype.next
Returns next item in the [property_name, property_value] format. It throws StopIteration if there are no more items.

Examples

Iterating over properties of an object

var a = {
  x: 10,
  y: 20,
};
var iter = Iterator(a);
console.log(iter.next()); // ["x", 10]
console.log(iter.next()); // ["y", 20]
console.log(iter.next()); // throws StopIteration

Iterate over properties of an object with legacy destructuring for-in statement

var a = {
  x: 10,
  y: 20,
};

for (var [name, value] in Iterator(a)) {
  console.log(name, value);   // x 10
                              // y 20
}

See also

Revision Source

<div>
 {{jsSidebar("Objects")}}</div>
<div class="warning">
 <strong>Non-standard.</strong> The <code><strong>Iterator</strong></code> function 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="Summary">Summary</h2>
<p>The <code><strong>Iterator</strong></code> function returns an object which implements legacy iterator protocol and iterates over enumerable properties of an object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
Iterator(<var>object</var>)</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>object</code></dt>
 <dd>
  Object to iterate over properties.</dd>
</dl>
<h2 id="Description">Description</h2>
<p>An overview of the usage is available on the <a href="/en-US/docs/JavaScript/Guide/Iterators_and_Generators" title="/en-US/docs/JavaScript/Guide/Iterators_and_Generators">Iterators and Generators</a> page.</p>
<h2 id="Properties">Properties</h2>
<h2 id="Methods">Methods</h2>
<dl>
 <dt>
  <code><strong>Iterator.prototype.next</strong></code></dt>
 <dd>
  Returns next item in the <code>[property_name, property_value]</code> format. It throws <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/StopIteration">StopIteration</a></code> if there are no more items.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<h3>Iterating over properties of an object</h3>
<pre class="brush: js">
var a = {
  x: 10,
  y: 20,
};
var iter = Iterator(a);
console.log(iter.next()); // ["x", 10]
console.log(iter.next()); // ["y", 20]
console.log(iter.next()); // throws StopIteration
</pre>
<h3>Iterate over properties of an object with legacy destructuring <code>for-in</code> statement</h3>
<pre class="brush: js">
var a = {
  x: 10,
  y: 20,
};

for (var [name, value] in Iterator(a)) {
  console.log(name, value);   // x 10
                              // y 20
}
</pre>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/JavaScript/Guide/Iterators_and_Generators" title="/en-US/docs/JavaScript/Guide/Iterators_and_Generators">Iterators and Generators</a></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/StopIteration">StopIteration</a></code></li>
</ul>
Revert to this revision