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

for...in - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

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 2015 ..

for...in . .

In this article

for (variable in object) {...
}
variable

() .

object

,

for...in . , , Array Object Object.prototype String.prototype, , String- indexOf(), Object - toString(). , , ( ).

,

for...in (. delete , ). , , . , , , . . , , , . , , , .

for...in

: for...in Array, .

- , . , for...in . for...in , .

, . for, Array.prototype.forEach() for...of, , .

, , getOwnPropertyNames(), hasOwnProperty() propertyIsEnumerable. , , , .

. .

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

for (var prop in obj) {
  console.log("obj." + prop + " = " + obj[prop]);
}

// :
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

hasOwnProperty():

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

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

ColoredTriangle.prototype = triangle;

var obj = new ColoredTriangle();

for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    console.log("obj." + prop + " = " + obj[prop]);
  }
}

// :
// "obj.color = red"

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

:

SpiderMonkey 40, (i = 0) for...in:

js
var obj = { a: 1, b: 2, c: 3 };
for (var i = 0 in obj) {
  console.log(obj[i]);
}
// 1
// 2
// 3

40 , SyntaxError ("for-in loop head declarations may not have initializers") (Firefox bug 748550 Firefox bug 1164741).

, v8 (Chrome), Chakra (IE/Edge) JSC (WebKit/Safari) .


Web Proxy Viewer  |  New URL  |  Original Page