diff options
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/iterador')
| -rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/iterador/index.html | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/iterador/index.html b/files/pt-br/web/javascript/reference/global_objects/iterador/index.html new file mode 100644 index 0000000000..1d00706e61 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/iterador/index.html @@ -0,0 +1,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> |
