Book HomeActionScript: The Definitive GuideSearch this book

8.5. The for-in Loop

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, "Objects and Classes".

Rather than repeating a series of statements until a given test expression yields the value 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;  // Statements typically use thisProp in some way
}

The substatements are executed once for each property of object; object is the name of any valid object; thisProp is any variable name or identifier name. During each loop iteration, the thisProp variable temporarily holds 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 a script that lists the properties of an object. Here we create an object and then itemize 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.

WARNING

Input text fields without a default value are not enumerated by a for-in loop. Hence, form-validation code that detects empty text fields will not work properly unless those text fields are explicitly declared as normal variables in the timeline upon which they reside. See Section 18.7, "Empty Text Fields and the for-in Statement" in Chapter 18, "On-Screen Text Fields".

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; // Statements 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]);
}


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.