aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/javascript/reference/global_objects/iterator/index.html
blob: 0a55a44af9b7d40734cd382a8b21d582869cb9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
title: Iterator
slug: Web/JavaScript/Reference/Global_Objects/Iterator
tags:
  - JavaScript
  - Reference
translation_of: Archive/Web/Iterator
---
<div>{{jsSidebar("Objects")}}</div>

<div> </div>

<div class="blockIndicator warning">
<p><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="https://developer.mozilla.org/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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">iterator protocol</a>.</p>
</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>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,  otherwise, if <code>keyOnly</code> is truthy, it returns <code>property_name</code> for each iteration.  If <code>object</code> is the <code>Iterator</code> instance or Generator instance, it returns <code>object</code> itself.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt><code><strong>Iterator.prototype[@@iterator]</strong></code></dt>
 <dd>Returns a function that returns iterator object.</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="https://developer.mozilla.org/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><code>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</code></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><code>var a = {
  x: 10,
  y: 20,
};

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

<h3 id="Iterating_with_for-of">Iterating with <code>for-of</code></h3>

<pre><code>var a = {
  x: 10,
  y: 20,
};

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

<h3 id="Iterates_over_property_name">Iterates over property name</h3>

<pre><code>var a = {
  x: 10,
  y: 20,
};

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

<h3 id="Passing_Generator_instance">Passing Generator instance</h3>

<pre><code>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
}</code></pre>

<h3 id="Passing_Iterator_instance">Passing Iterator instance</h3>

<pre><code>var a = {
  x: 10,
  y: 20,
};

var i = Iterator(a);

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

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

<p>Non-standard. Not part of any current standards document</p>

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

<p>Not supported. Used to be in Firefox in versions prior to Firefox 57.</p>

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

<ul>
 <li><a href="https://developer.mozilla.org/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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/StopIteration">StopIteration</a></code></li>
</ul>