A for-in statement is a specialized loop used to list the properties of an object. New programmers may want to skip this section for now and return to it after reading Chapter 12.
Unlike other loops, which repeat a series of statements until a given test expression is false, a for-in loop iterates once for each property in the specified object. Therefore, for-in statements do not need an explicit update statement because the number of loop iterations is determined by the number of properties in the object being inspected. The syntax of a for-in loop looks like this:
for (var thisProp in object) { substatements // Substatements typically use thisProp in some way }
The substatements are executed once for each property of object; object is a reference to any valid object; thisProp is a variable name (and should not be hardcoded with the name of an existing object property). During each loop iteration, the thisProp variable is automatically set to a string that is the name of the object property currently being enumerated. That string value can be used during each iteration to access and manipulate the current property. The simplest example of a for-in loop is one that lists the properties of an object. Here we create an object and then enumerate its properties with a for-in loop:
var ball = new Object( ); ball.radius = 12; ball.color = "red"; ball.style = "beach"; for (var prop in ball) { trace("ball has the property " + prop); }
Because prop stores the names of the properties of ball as strings, we can use prop with the [ ] operator to retrieve the values of those properties, like this:
for (var prop in ball) { trace("ball." + prop + " is " + ball[prop]); }
Retrieving property values with a for-in loop also provides a super way to detect the movie clips present on a timeline. For a demonstration of the for-in loop used as a movie clip detector, see Example 3-1.
Note that the properties of the object being inspected in a for-in loop are not enumerated in any predictable order. Also, for-in statements do not always list every property of an object. When the object is user-defined, all properties are enumerated, including any inherited properties. But some properties of built-in objects are skipped by the for-in statement. Methods of built-in objects, for example, are not enumerated by a for-in loop. If you want to use a for-in statement to manipulate the properties of a built-in object, first build a test loop to determine the object's accessible properties, such as:
thisFormat = new TextFormat( ); for (var prop in thisFormat) { trace("Property found: " + prop); }
When the object being inspected is an array, the value of thisProp is a string containing the integer index of the element being enumerated.
To prevent a user-defined object property from appearing in a for-in loop, you can use the undocumented ASSetPropFlags( ) function as follows:
// Create a new ball object with radius and color properties. var ball = new Object( ); ball.radius = 10; ball.color = "red"; // Hide ball.radius from for-in loops. ASSetPropFlags(ball, ["radius"], 1); // Displays: color (radius is hidden) for (var prop in ball) { trace(prop); }
This technique is not officially documented by Macromedia and may be replaced by a more intuitive function in the future. It is available in Flash Player 5 and Flash Player 6. For more information, see:
|
The for-in statement can also be used to extract elements in an array, in which case it takes the form:
for (var thisElem in array) { substatements; // Substatements typically use thisElem in some way }
This example lists the elements of an array:
var myArr = [123, 234, 345, 456]; for (var elem in myArr) { trace(myArr[elem]); }