[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/for...in [Back]  [Original]

for...in - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

for...in

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

Statement
Implemented in: JavaScript 1.0
ECMA Version: ECMA-262

In this article

for ( in ) {...
}

A different property name is assigned to variable on each iteration.

Object whose enumerable properties are iterated.

Description

for...in (ArrayObject) Object.prototype String.prototype StringindexOf Object toString ()

A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not 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 (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.

for..in for...in JavaScript for ( Array.forEach for...of )

Examples

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

js
var o = { a: 1, b: 2, c: 3 };

function show_props(obj, objName) {
  var result = "";

  for (var prop in obj) {
    result += objName + "." + prop + " = " + obj[prop] + "\n";
  }

  return result;
}

alert(
  show_props(o, "o"),
); /* alerts (in different lines): o.a = 1 o.b = 2 o.c = 3 */

The following function illustrates the use of hasOwnProperty: the inherited properties are not displayed.

js
var triangle = { a: 1, b: 2, c: 3 };

function ColoredTriangle() {
  this.color = "red";
}

ColoredTriangle.prototype = triangle;

function show_own_props(obj, objName) {
  var result = "";

  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      result += objName + "." + prop + " = " + obj[prop] + "\n";
    }
  }

  return result;
}

o = new ColoredTriangle();
alert(show_own_props(o, "o")); /* alerts: o.color = red */

Specification
ECMAScript 2027 LanguageSpecification
# sec-for-in-and-for-of-statements

See also


Web Proxy Viewer  |  New URL  |  Original Page