Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

    Implemented in: JavaScript 1.0, NES 2.0
    ECMA Version: ECMA-262


    for (variable in object) {


    A different property name is assigned to variable on each iteration.
    Object whose enumerable properties are iterated.


    A loop does not iterate over non–enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype that are not enumerable, such as String's indexOf method or Object's toString method. The loop will iterate over all enumerable properties of the object or that it inherits from its constructor's proptotype (including any which overwrite built-in properties).

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

    Where only the properties of the object should be considered, a hasOwnProperty check should be performed to ensure that only properties of the object and not inherited properties are used (propertyIsEnumerable can also be used but is not intuitive).

    In some cases, built–in prototypes be usefully extended by the user where there are no concerns caused by inclusion of other code that may use  and not perform the above checks. should not be used to iterate over an Array where 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 when iterating over arrays where the order of access is important.


    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.

    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 */ 

    Iterating over only 



    此页面的贡献者有: ziyunfei, teoli
    最后编辑者: teoli,