ActionScript is an object-based language. The entire language is composed primarily of blueprints, called classes, for different types of objects. Based on these blueprints you, as the developer, can construct many instances of many types of objects, and each instance automatically inherits all the characteristics and functionality of its class. For example, all arrays are members of the Array class, which defines the length property as well as the methods (concat( ), sort( ), etc.) that all arrays inherit.

Therefore, whenever you are using ActionScript, you are inherently using objects and object-oriented programming. Understanding the inner workings of ActionScript classes and objects puts you well on your way to harnessing the true power of the language. This knowledge enables you to add properties and methods to individual instances (objects) and to the classes they inherit from.

Every class in ActionScript has a prototype property. The prototype property contains an object from which all instances of the class inherit properties and methods.

If you add a property or method to a class's prototype, all instances of that class automatically inherit that custom property or method.

Here is an example:

// Add a property to the Array class.
Array.prototype.testProperty = "this is a test property";

// Create a new array. This array inherits, along with all of the other properties
// and methods of the Array class, the custom testProperty property.
myArray = new Array(  ); 

// Displays: "this is a test property"

Here is another example in which we add a method to a class's prototype:

// Add the testMethod(  ) method to the Array class.
Array.prototype.testMethod = function (  ) {
        trace("This is a test method.");

// Create a new array. All arrays now inherit the testMethod(  ) method.
myArray = new Array(  );

// Displays: "This is a test method."
myArray.testMethod(  );

Prototype-based inheritance works as follows. When a property is accessed or a method is invoked from an object, the ActionScript interpreter checks if that property or method is explicitly defined for the object. If not, ActionScript looks for it on the object's class's prototype property. Classes can even inherit from other classes. This is known as subclassing or extending a superclass. Therefore, if ActionScript cannot find a method or property in the class's prototype property, it looks next in the superclass's prototype property. This same process is followed until ActionScript finds the method or property, or until it reaches the final superclass. The Object class is the superclass of all other classes, so any properties or methods added to the Object class are automatically inherited by every object of any type:

// Add a property to the Object class.
Object.prototype.objectTextProperty = "this is a property for all objects";
// Create a new Date object.
myDate = new Date(  );
// Displays: "this is a property for all objects"

However, a subclass can implement a property or method of the same name, effectively obscuring the property or method found in the superclass. For example, although the Object class implements a toString( ) method, the Date class implements its own custom toString( ) method that is more appropriate for its needs:

// Displays: [object Object]
trace (new Object().toString(  ));
// Displays: Mon May 26 14:17:26 GMT-0400 2003
trace (new Date().toString(  ));

See Also

Recipe 12.8, Recipe 12.9, and Recipe 12.12

    Part I: Local Recipes
    Part II: Remote Recipes