6.9. for/inThe for keyword is used in two ways in JavaScript. We've just seen how it is used in the for loop. It is also used in the for/in statement. This statement is a somewhat different kind of loop with the following syntax: for (variable in object) statement variable should be either the name of a variable, a var statement declaring a variable, an element of an array, or a property of an object (i.e., it should be something suitable as the leftthand side of an assignment expression). object is the name of an object or an expression that evaluates to an object. As usual, statement is the statement or statement block that forms the body of the loop. You can loop through the elements of an array by simply incrementing an index variable each time through a while or for loop. The for/in statement provides a way to loop through the properties of an object. The body of the for/in loop is executed once for each property of object. Before the body of the loop is executed, the name of one of the object's properties is assigned to variable, as a string. Within the body of the loop, you can use this variable to look up the value of the object's property with the [] operator. For example, the following for/in loop prints the name and value of each property of an object: for (var prop in my_object) { document.write("name: " + prop + "; value: " + my_object[prop], "<br>"); } Note that the variable in the for/in loop may be an arbitrary expression, as long as it evaluates to something suitable for the lefthand side of an assignment. This expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you could use code like the following to copy the names of all object properties into an array: var o = {x:1, y:2, z:3}; var a = new Array( ); var i = 0; for(a[i++] in o) /* empty loop body */; JavaScript arrays are simply a specialized kind of object. Therefore, the for/in loop enumerates array indexes as well as object properties. For example, following the previous code block with this line enumerates the array "properties" 0, 1, and 2: for(i in a) alert(i); The for/in loop does not specify the order in which the properties of an object are assigned to the variable. There is no way to tell what the order will be in advance, and the behavior may differ between implementations or versions of JavaScript. If the body of a for/in loop deletes a property that has not yet been enumerated, that property will not be enumerated. If the body of the loop defines new properties, whether or not those properties will be enumerated by the loop is implementation-dependent. The for/in loop does not actually loop through all possible properties of all objects. In the same way that some object properties are flagged to be read-only or permanent (nondeletable), certain properties are flagged to be nonenumerable. These properties are not enumerated by the for/in loop. While all user-defined properties are enumerated, many built-in properties, including all built-in methods, are not enumerated. As we'll see in Chapter 8, objects can inherit properties from other objects. Inherited properties that are user-defined are also enumerated by the for/in loop. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|