aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/iterador/index.html
blob: 1d00706e6191f9cbb70d9b2f4d472b3c89aa5567 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
title: Iterator
slug: Web/JavaScript/Reference/Global_Objects/Iterador
translation_of: Archive/Web/Iterator
---
<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>A função <code><strong>Iterator</strong></code> retorna um objeto que implementa o protocolo legado do iterador e itera sobre propriedades enumeraveis do objeto.</p>

<h2 id="Syntax">Syntax</h2>

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

<h3 id="Parametros">Parametros</h3>

<dl>
 <dt><code>Objeto</code></dt>
 <dd><span style="background-color: #ffffff; font-size: 1rem; font-style: inherit; font-weight: inherit; letter-spacing: -0.00278rem;">Objeto que itera sobre as propriedades</span></dd>
 <dd>Se <code>keyOnly</code> for um valor verdadeiro, <code>Iterator.prototype.next</code> retorna somente o <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">nome_da_propriedade</span></font>.</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 {{jsxref("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, 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 id="Iterating_with_for-of">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 id="Iterates_over_property_name">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 id="Passing_Generator_instance">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 id="Passing_Iterator_instance">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>Non-standard. Not part of any current standards document.</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>