
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
280
|
Chapter 12: Objects and Classes
In this case, the [] operator is less concise than the dot operator and offers no appar-
ent benefit. The
[] operator is useful when the property name is a complex expres-
sion. Next, rather than using a string literal, we specify the property name with an
expression that yields the string “radius”:
var prop = "radius"; // Set a variable that contains the property name.
trace(redBall[prop]); // The variable prop contains the
// property name "radius".
Built-in ActionScript properties are also accessed via the dot and [] operators. Here
we access the value of the Math object’s PI property, first using the dot operator,
then using the
[] operator:
trace(Math.PI);
trace(Math["PI"]);
However, in pure OOP, we’ll almost never access an object’s properties directly;
instead, we’ll use methods to access property values. For example, to check the
volume property of an instance of the built-in Sound class, we use:
trace(mySound.getVolume( ));
not:
trace(mySound.volume); // INCORRECT!
Remember that the dot operator requires us to specify a property as an
identifier, not a string, whereas the [ ] operator requires a string, not
an identifier.
Using a for-in Loop to Iterate over an Object’s Properties
In Chapter 8, you learned that a for-in loop can be used to enumerate ...