
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Properties
|
79
// Initialize area. This is perfectly legal within a constructor.
area = width * height;
}
}
Enumerating Properties with for-in loops
Even if you are familiar with core ActionScript 1.0 syntax such as for-in loops (and
this book assumes you are), you may not be aware of their subtle interaction with
object-oriented aspects of the language. Specifically, although the for-in statement lets
us list (a.k.a. enumerate) properties of an object, not all properties are enumerable.
The following example displays the enumerable properties of
obj in the Output panel:
for (var prop:String in obj) {
trace("property name: " + prop);
trace("property value: " + obj[prop]);
}
By default, instance properties (i.e., properties not declared as static) are not enumer-
able with a for-in loop. For example, suppose our Box class defines two public prop-
erties,
width and height, and initializes them both to the value 15:
class Box {
public var width:Number = 15;
public var height:Number = 15;
}
When we create a new Box object, its width and height properties are set to 15:
var b:Box = new Box( );
trace(b.width); // Displays: 15
trace(b.height); // Displays: 15
But when we enumerate the properties of the Box object, width and height are not
listed:
// This loop generates no output
for (var prop:String ...