12.3 Object Properties

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 12.  Objects and Classes

As you learned earlier, properties are named data containers associated with an object. They are defined once by an object's class but set individually for each object instance. Like variables, object properties can contain any kind of data strings, numbers, Booleans, null, undefined, functions, arrays, movie clips, or even other objects.

12.3.1 Referring to Properties

The familiar dot operator gives us access to an object's properties. We separate the name of the property from the object it belongs to using a dot (a period), as follows:

objectName.propertyName

where objectName is the name of our object and propertyName must be a legal identifier that names a property of objectName.

For example, if we have a redBall object instance with a radius property, we can retrieve the value of radius and assign it to a variable, r, as follows:

var r = redBall.radius;

Or, to set redBall's radius property to 50, we can use:

redBall.radius = 50;

Alternatively, we can refer to a property using the [ ] operator, as follows:

objectName[propertyNameString]

The [ ] operator allows us to compose a property name using any expression that evaluates to a string. For example, here we first retrieve and then assign the value of radius, achieving the same results as our previous example:

var r = redBall["radius"];  // Retrieve redBall["radius"] = 50;     // Assign

In this case, the [ ] operator is less concise than the dot operator and offers no apparent benefit. The [ ] operator is useful when the property name is a complex expression. 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.

12.3.2 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 the properties of an object. Now that you know a little more about objects, it's worth returning to the for-in statement briefly to review how it can be used to iterate over (i.e., access each one of) an object's properties.

Like all loops, the for-in statement includes a header and a body. The body of a for-in statement is automatically executed once for each property in the specified object. We don't need to know the number of properties or their names in advance, because as each cycle of the loop executes, our "iterator" variable automatically becomes the name of the next property. We can therefore iterate over the properties of the object, like this:

// List all the properties of the ball object for (var prop in ball) {   trace("Property " + prop + " has the value " + ball[prop]); }

Note that the iterator variable, prop, in this example is not an integer, as it would be in a for loop. That is, don't confuse a standard for loop, often used for accessing numbered array elements, with a for-in loop used to access an object's properties. For more information on for-in loops, including how to prevent an object's properties from being exposed to for-in, see Chapter 8.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net