12.6 Using Standalone Object Instances as Associative Arrays

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

As we saw earlier, ActionScript leniently allows methods to assign new properties to objects. In fact, ActionScript is even more flexible than that it allows any code to create a new property or method on a single object instance (rather than on all instances of a class). For example, the following code creates an instance, obj, of the generic Object class and immediately assigns it the new properties x (set to 10) and y (set to 20):

var obj = new Object( ); obj.x = 10; obj.y = 20;

Assigning properties and methods to individual objects is, of course, bad OOP practice. But in ActionScript, it is also a completely legitimate way to store a group of named values (much like a hash or associative array). In fact, many of ActionScript's built-in methods take generic objects as parameters. For example, MovieClip.globalToLocal( ) takes a point object parameter that must supply an x property and a y property. The x and y properties represent a global position that will be converted to a movie clip's local coordinate system.

pt = new Object( );  // Create generic object to hold our point pt.x = 0;          // Left border of main Stage pt.y = 0;                          // Top border of main Stage this.globalToLocal(pt);          // Convert pt to local coordinates trace("From the current clip, the top-left corner of the main Stage is at "); trace("x: " + pt.x + "y: " + pt.y);

Similarly, MovieClip.attachMovie( ) takes an initObj parameter used to transfer an object's properties to a new movie clip instance, car_mc, as follows:

// Create an object var initObj = new Object( );     // Create properties on the object initObj.speed = 3; initObj.onEnterFrame = function ( ) {   this._x += this.speed; };     // Transfer the object's properties to car_mc this.attachMovie("CarSymbol", "car_mc", 1, initObj);

Notice that this code defines onEnterFrame( ) as a method on initObj, which, given the task at hand, is perfectly legal (and convenient!).

12.6.1 Object Literals

To create a generic object of the Object class directly in our code without using the Object constructor, we can use an object literal, just as we might use a string literal or an array literal. An object literal is a series of comma-separated property name/value pairs, enclosed in curly braces. Here's the general syntax:

{ property1: value1, property2: value2, property3: value3 }

The names of properties in an object literal must be legal identifiers, as described in Chapter 14. The values can be any valid expression. For example:

// An object literal with two numeric properties myObject = { x: 30+10, y: 20*2 };

Object literals are used for the sake of terseness. For example, in the following code we transfer the speed and onEnterFrame properties to car_mc more succinctly (though perhaps less legibly) than we did earlier:

this.attachMovie("CarSymbol", "car_mc", 1, {speed:3, onEnterFrame: function ( ) {                                                      this._x += this.speed;}});
     



    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