Visit Mozilla.org

Dokumentacja języka JavaScript 1.5:Polecenia:for...in

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Spis treści

[edytuj] Podsumowanie

Iterates a specified variable over all the properties of an object. For each distinct property, a specified statement is executed.

Statement
Zimplementowane w: JavaScript 1.0, NES 2.0
Wersja ECMA: ECMA-262

[edytuj] Składnia

for (variable in object)
   statement

[edytuj] Parametry

variable 
Variable to iterate over every property, optionally declared with the var keyword. This variable is local to the function, not to the loop.
object 
Object for which the properties are iterated.
statement 
A statement to execute for each property. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

[edytuj] Opis

Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexOf method. However, all user-defined properties are iterated over.

[edytuj] Przykłady

[edytuj] Przykład: Użycie 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;
}

[edytuj] Zobacz także

for