mozilla

for...in

这篇翻译不完整。请帮忙从英语翻译这篇文章

概述

以任意序迭代一个对象的可枚举属性。每个不同的属性,语句都会被执行一次。

语法

for (variable in object) {
  ...
} 

参数

variable
每次迭代,一个不同的属性名将会赋予 variable 
object
可枚举属性被迭代的对象

描述

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

删除,添加或者修改属性

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

Array iteration and for...in

Note: for..in 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 for...in 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)
for...in statement
Standard  
ECMAScript 6 (ECMA-262)
for...in 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 for...in loop:
    var obj = {a:1, b:2, c:3};
    for(var i=0 in obj) {
      console.log(obj[i]);
    }
    // 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,
隐藏侧边栏