Recipe 12.5 Creating a Custom Class

12.5.1 Problem

You want to create a custom ActionScript class to implement custom functionality or encapsulate related properties.

12.5.2 Solution

Create a constructor function and then assign properties and methods to its prototype.

12.5.3 Discussion

You might want to implement a custom class to encapsulate a series of methods and properties with related functionality. For example, you might implement a custom class that communicates with an external application. To create a custom class, first define a constructor function. The constructor function is defined in the typical fashion?either as a named function or as an anonymous function assigned to a variable. The latter technique is useful for creating globally accessible classes. Classes defined with named functions are accessible only within the scope of the timeline in which they are defined or by using the fully qualified target path.

Here are two degenerate examples. Obviously, constructor functions do not need to be empty; they can take parameters and they can have function bodies.

// Create a local class named MyClass.
function MyClass (  ) {}

// Create a global class named MyClass.
_global.MyClass = function (  ) {};

By convention, class names are capitalized. ActionScript is case-insensitive, so it is not technically an issue, but following this naming convention helps you easily distinguish classes from objects (instances of a class). Furthermore, the ECMA-262 standard, on which JavaScript is based, demands case-sensitivity. So by adopting the JavaScript convention, you make it easier for other developers to understand or port your code.

Typically, a constructor function's body initializes properties of that class. The this keyword, when used within a constructor (or any of the class methods), is a reference to the instance of the class from which it is invoked:

_global.MyClass = function (param1) {
  // Stored the passed-in parameter in a property of the object
  this.myProperty1 = param1;

You can add methods to a class using the same technique as shown in Recipe 12.4. However, assign the method to a property of the class's prototype so that the method is inherited by all instances of that class (i.e., all objects derived from the class).

MyClass.prototype.myMethod = function (  ) {

You can create instances of the class by calling the constructor function using the new keyword, as discussed in Recipe 12.2. For example:

// Define the class constructor for the MyClass class. It accepts one parameter and
// assigns its value to the myProperty1 property, which is unique for every instance.
_global.MyClass = function (param1) {
  this.myProperty1 = param1;

// Define a custom method for the class.
MyClass.prototype.myMethod = function (  ) {

// Create an instance of MyClass and pass a parameter to assign to myProperty1.
MyClassInstance0 = new MyClass("this is one instance");

// Invoke the myMethod(  ) method of the object. 
myClassInstance0.myMethod(  );    // Displays: "this is one instance"

// Create a second instance of MyClass.
MyClassInstance1 = new MyClass("this is another instance");

// Invoke myMethod(  ) on the second object. Note its unique value for myProperty1.
myClassInstance1.myMethod(  );    // Displays: "this is another instance"

12.5.4 See Also

Recipe 12.2

    Part I: Local Recipes
    Part II: Remote Recipes