• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 8. Shared Objects > Objects and Shared Objects

8.1. Objects and Shared Objects

ActionScript objects are implemented as associative arrays. Each object property associates a value with a property name. The property name is a string that is used to store and retrieve a value in a slot, or location, within the object. Objects are similar to arrays—they have slots that hold values. Arrays are designed to provide access to the value in each slot via an index number, while objects provide access to values via a string property name. Example 8-1 reviews how to add properties to a generic ActionScript object.

Example 8-1. Creating and adding properties to an ActionScript object

// Create a simple object named user.
user = new Object( );
// Use dot notation to add a userName property.
user.userName = "blesser";
// Use the [] operator to add a password property.
user["password"] = "bigSecret";
// Add a function as a property of this generic object.
user.showProperties = function ( ) {
  for (var prop in this) {
    trace(prop + ": " + this[prop]);
  }
};
// Call the function.
user.showProperties( );

// Output to the Output panel for this script:
showProperties: [type Function]
password: bigSecret
userName: blesser


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint