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

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Iterator
  • Revision title: Iterator
  • Revision id: 946339
  • Created:
  • Creator: arai
  • Is current revision? Yes
  • Comment Add documentation about keyOnly argument, interaction with Generator, Iterator instance, and iterator protocol.

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.

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

Syntax

Iterator(object, [keyOnly])

Parameters

object
Object to iterate over properties.
keyOnly
If keyOnly is truthy value, Iterator.prototype.next returns property_name only.

Description

Returns Iterator instance that iterates over object. Iterator instance returns [property_name, property_value] array for each iteration if keyOnly is falsy,  otherwise, if keyOnly is truthy, it returns property_name for each iteration.  If object is the Iterator instance or {{jsxref("Generator")}} instance, it returns object itself.

Properties

Iterator.prototype[@@iterator]
Returns a function that returns iterator object, that conforms to {{jsxref("Iteration_protocols", "iterator protocol", "", 1)}}.

Methods

Iterator.prototype.next
Returns next item in the [property_name, property_value] format or property_name only. 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

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

Iterating with for-of

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

for (var [name, value] of Iterator(a)) {  // @@iterator is used
  console.log(name, value);   // x 10
                              // y 20
}

Iterates over property name

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

for (var name in Iterator(a, true)) {
  console.log(name);   // x
                       // y
}

Passing Generator instance

function f() {
  yield "a";
  yield "b";
}
var g = f();

console.log(g == Iterator(g)); // true

for (var v in Iterator(g)) {
  console.log(v);   // a
                    // b
}

Passing Iterator instance

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

var i = Iterator(a);

console.log(i == Iterator(i)); // true

Specifications

{{WhyNoSpecStart}}Non-standard. Not part of any current standards document.{{WhyNoSpecEnd}}

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

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>

<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">Syntax</h2>

<pre class="syntaxbox">
Iterator(<var>object</var>, [keyOnly])</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>object</code></dt>
 <dd>Object to iterate over properties.</dd>
 <dt><code>keyOnly</code></dt>
 <dd>If <code>keyOnly</code> is truthy value, <code>Iterator.prototype.next</code> returns <code>property_name</code> only.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>Returns <code>Iterator</code> instance that iterates over <code>object</code>. <code>Iterator</code> instance returns <code>[property_name, property_value]</code> array for each iteration if <code>keyOnly</code> is falsy,&nbsp; otherwise, if <code>keyOnly</code> is truthy, it returns <code>property_name</code> for each iteration.&nbsp; If <code>object</code> is the <code>Iterator</code> instance or {{jsxref("Generator")}} instance, it returns <code>object</code> itself.</p>

<h2>Properties</h2>

<dl>
 <dt><code><strong>Iterator.prototype[@@iterator]</strong></code></dt>
 <dd>Returns a function that returns iterator object, that conforms to {{jsxref("Iteration_protocols", "iterator protocol", "", 1)}}.</dd>
</dl>

<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 or <code>property_name</code> only. 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 id="Iterating_over_properties_of_an_object">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 id="Iterating_over_properties_of_an_object_with_legacy_destructuring_for-in_statement">Iterating 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>

<h3>Iterating with <code>for-of</code></h3>

<pre class="brush: js">
var a = {
  x: 10,
  y: 20,
};

for (var [name, value] of Iterator(a)) {  // @@iterator is used
  console.log(name, value);   // x 10
                              // y 20
}
</pre>

<h3>Iterates over property name</h3>

<pre class="brush: js">
var a = {
  x: 10,
  y: 20,
};

for (var name in Iterator(a, true)) {
  console.log(name);   // x
                       // y
}
</pre>

<h3>Passing Generator instance</h3>

<pre class="brush: js">
function f() {
  yield "a";
  yield "b";
}
var g = f();

console.log(g == Iterator(g)); // true

for (var v in Iterator(g)) {
  console.log(v);   // a
                    // b
}
</pre>

<h3>Passing Iterator instance</h3>

<pre class="brush: js">
var a = {
  x: 10,
  y: 20,
};

var i = Iterator(a);

console.log(i == Iterator(i)); // true
</pre>

<h2 id="Specifications">Specifications</h2>

<p>{{WhyNoSpecStart}}Non-standard. Not part of any current standards document.{{WhyNoSpecEnd}}</p>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<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