for..in

Availability

Flash Player 5.

Usage

for(variableIterant in object){
  statement(s); 
}

Parameters

variableIterant The name of a variable to act as the iterant, referencing each property of an object or element in an array.

object The name of an object to be repeated.

statement(s) An instruction to execute for each iteration.

Returns

Nothing.

Description

Statement; loops through the properties of an object or element in an array, and executes the statement for each property of an object.

Some properties cannot be enumerated by the for or for..in actions. For example, the built-in methods of the Array class (such as Array.sort() and Array.reverse()) are not included in the enumeration of an Array object, and movie clip properties, such as _x and _y, are not enumerated. In external class files, instance members are not enumerable; only dynamic and static members are enumerable.

The for..in statement iterates over properties of objects in the iterated object's prototype chain. If the child's prototype is parent, iterating over the properties of the child with for..in, will also iterate over the properties of parent.

The for..in action enumerates all objects in the prototype chain of an object. Properties of the object are enumerated first, then properties of its immediate prototype, then properties of the prototype's prototype, and so on. The for..in action does not enumerate the same property name twice. If the object child has prototype parent and both contain the property prop, the for..in action called on child enumerates prop from child but ignores the one in parent.

Example

The following is an example of using for..in to iterate over the properties of an object:

myObject = { name:'Tara', age:27, city:'San Francisco' };
for (name in myObject) {
  trace ("myObject." + name + " = " + myObject[name]);
}

The output of this example is as follows:

myObject.name = Tara
myObject.age = 27
myObject.city = San Francisco

The following is an example of using the typeof operator with for..in to iterate over a particular type of child:

for (name in my_mc) {
  if (typeof (my_mc[name]) = "movieclip") {
    trace ("I have a movie clip child named " + name);
  }
}

The following example enumerates the children of a movie clip and sends each to Frame 2 in their respective Timelines. The RadioButtonGroup movie clip is a parent with several children, _RedRadioButton_, _GreenRadioButton_ and _BlueRadioButton.

for (var name in RadioButtonGroup) {
  RadioButtonGroup[name].gotoAndStop(2);
}