for...in

  • Enlace amigable (slug) de la revisión: Referencia_de_JavaScript_1.5/Sentencias/for...in
  • Título de la revisión: for...in
  • Id de la revisión: 133657
  • Creada:
  • Creador: Talisker
  • ¿Es la revisión actual? No
  • Comentario /* Vea También */

Contenido de la revisión

Resumen

Itera con una variable especificada sobre todas las propiedades de un objeto, en un orden arbitriario. Para cada una de las propiedades, se ejecuta la sentencia especificada.

Sentencia
Implementada en: JavaScript 1.0, NES 2.0
Versión ECMA: ECMA-262

Sintaxis

for (variable in objeto)
  sentencia
for (var variable in objeto)
  sentencia

Parámetros

variable 
Variable to iterate over every property, optionally declared with the var keyword. This variable is local to the function, not to the loop. (In other words, when the loop completes the variable has the value of the last property visited.)
objeto 
Object for which the properties are iterated.
sentencia 
A statement to execute for each property. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

Descripción

A for...in loop does not iterate over built-in properties. These include all built-in methods of objects, such as String's indexOf method or Object's toString method. However, the loop will iterate over all user-defined properties (including any which overwrite built-in properties).

A for...in loop iterates over the properties of an object in an arbitrary order. If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.

Ejemplos

Ejemplo: Using for...in

The following function takes as its arguments an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.

function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}

Vea También

{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Statements/for...in", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for...in", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/for...in", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for...in" } ) }}

Fuente de la revisión

<p>
</p>
<h3 name="Resumen"> Resumen </h3>
<p>Itera con una variable especificada sobre todas las propiedades de un objeto, en un orden arbitriario. Para cada una de las propiedades, se ejecuta la sentencia especificada.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Sentencia</td>
</tr>
<tr>
<td>Implementada en:</td>
<td>JavaScript 1.0, NES 2.0</td>
</tr>
<tr>
<td>Versión ECMA:</td>
<td>ECMA-262
</td>
</tr>
</tbody></table>
<h3 name="Sintaxis"> Sintaxis </h3>
<pre class="eval">for (<i>variable</i> in <i>objeto</i>)
  <i>sentencia</i>
</pre>
<pre class="eval">for (var <i>variable</i> in <i>objeto</i>)
  <i>sentencia</i>
</pre>
<h3 name="Par.C3.A1metros"> Parámetros </h3>
<dl><dt> <code>variable</code> </dt><dd> Variable to iterate over every property, optionally declared with the <code>var</code> keyword. This variable is local to the function, not to the loop.  (In other words, when the loop completes the variable has the value of the last property visited.)
</dd></dl>
<dl><dt> <code>objeto</code> </dt><dd> Object for which the properties are iterated.  
</dd></dl>
<dl><dt> <code>sentencia</code> </dt><dd> A statement to execute for each property. To execute multiple statements within the loop, use a <a href="es/Referencia_de_JavaScript_1.5/Sentencias/block">block</a> statement (<code>{ ... }</code>) to group those statements.
</dd></dl>
<h3 name="Descripci.C3.B3n"> Descripción </h3>
<p>A <code>for...in</code> loop does not iterate over built-in properties. These include all built-in methods of objects, such as <code><a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/String">String</a></code>'s <code><a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/String/indexOf">indexOf</a></code> method or <code><a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object">Object</a></code>'s <code><a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/toString">toString</a></code> method.  However, the loop will iterate over all user-defined properties (including any which overwrite built-in properties).
</p><p>A <code>for...in</code> loop iterates over the properties of an object in an arbitrary order.  If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time.  A property which is deleted before it has been visited will not then be visited later.  Properties added to the object over which iteration is occurring may either be visited or omitted from iteration.  In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.
</p>
<div class="note">
<p>Although it may be tempting to use this as a way to iterate over an <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Array">Array</a>, this is a bad idea.  The <code>for...in</code> statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a <code>"foo"</code> property to it or even by adding a method or property to <code>Array.prototype</code>), the <code>for...in</code> statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order.  Thus it is better to use a traditional <a href="es/Referencia_de_JavaScript_1.5/Sentencias/for">for</a> loop with a numeric index when iterating over arrays.
</p>
</div>
<h3 name="Ejemplos"> Ejemplos </h3>
<h4 name="Ejemplo:_Using_for...in"> Ejemplo: Using <code>for...in</code> </h4>
<p>The following function takes as its arguments an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.
</p>
<pre class="eval">function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}
</pre>
<h3 name="Vea_Tambi.C3.A9n"> Vea También </h3>
<ul><li> <a href="es/Referencia_de_JavaScript_1.5/Sentencias/for_each...in">for each...in</a> - similar a <code>for...in</code>, pero itera sobre los valores de las propiedades del objeto, en vez de sobre los mismos nombres de las propiedades. (<a href="es/Novedades_en_JavaScript_1.6">Novedades en JavaScript 1.6</a>.)
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Sentencias/for">for</a>
</li></ul>
<div class="noinclude">
</div>
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Statements/for...in", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for...in", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/for...in", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for...in" } ) }}
Revertir a esta revisión