for (variable in object) {


每次迭代,一个不同的属性名将会赋予 variable 

描述 循环不遍历不可枚举属性。使用内建构造器例如 Array 和 Object 创建的对象拥有从 Object.prototype 和 String.prototype 继承的不可枚举属性,例如 String 的 indexOf 方法或者 Object 的 toString 方法。循环将迭代对象的所有可枚举属性,包括从它的构造函数的 prototype 继承而来的(包括被覆盖的内建属性)。

删除,添加或者修改属性 循环以任意序迭代一个对象的属性(浏览 delete operator 了解更多关于为什么开发者不能依赖于“看上去”的迭代顺序,至少是在跨浏览器情况下)。 如果一个属性在一次迭代中被修改,在稍后被访问,其在循环中的值是其在稍后时间的值。一个在被访问之前已经被删除的属性将不会在之后被访问。在迭代进行时被添加到对象的属性,可能在之后的迭代被访问,也可能被忽略。通常,在迭代过程中最好不要在对象上进行添加、修改或者删除属性的操作,除非是对当前正在被访问的属性。这里并不保证是否一个被添加的属性在迭代过程中会被访问到,不保证一个修改后的属性(除非是正在被访问的)会在修改前或者修改后被访问,不保证一个被删除的属性将会在它被删除之前被访问。

Array iteration and

Note: should not be used to iterate over an Array where the index order is important.

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.

Because the order of iteration is implementation dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.

Iterating over own properties only

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.



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];
    return result;

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

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

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("o." + prop + " = " + obj[prop]);

// Output:
// "o.color = red"


Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
ECMAScript 5.1 (ECMA-262) statement
ECMAScript 6 (ECMA-262) statement
Release Candidate  


Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Firefox-specific notes

  • Prior to SpiderMonkey 40 (Firefox 40 / Thunderbird 40 / SeaMonkey 2.37), it was possible to use an initializer expression (i=0) in a loop:
    var obj = {a:1, b:2, c:3};
    for(var i=0 in obj) {
    // 1
    // 2
    // 3
    This non-standard behavior has been removed in version 40 and later. It will throw a SyntaxError ("for-in loop head declarations may not have initializers") from now on (bug 748550).



向此页面作出贡献: teoli, ziyunfei, denghongcai, wonyun
最后编辑者: wonyun,