Writing Scripts with ActionScript > Using custom objects

Using custom objects

You can create custom objects to organize information in your scripts for easier storage and access by defining an object's properties and methods. After you create a master object or "class," you can use or "instantiate" copies (that is, instances) of that object in a movie. This allows you to reuse code and conserve file size.

An object is a complex data type containing zero or more properties. Each property, like a variable, has a name and a value. Properties are attached to the object and contain values that can be changed and retrieved. These values can be of any data type: string, number, Boolean, object, movie clip, or undefined. The following properties are of various data types:

customer.name = "Jane Doe";
customer.age = 30;
customer.member = true;
customer.account.currentRecord = 000609;
customer.mcInstanceName._visible = true;

The property of an object can also be an object. In line 4 of the previous example, account is a property of the object customer and currentRecord is a property of the object account. The data type of the currentRecord property is number.


 
Creating an object

You can use the new operator to create an object from a constructor function. A constructor function is always given the same name as the type of object it is creating. For example, a constructor that creates an account object would be called Account. The following statement creates a new object from the function called MyConstructorFunction:

new MyConstructorFunction (argument1, argument2, ... argumentN);

When MyConstructorFunction is called, Flash passes it the hidden argument this, which is a reference to the object that the MyConstructorFunction is creating. When you define a constructor, this allows you to refer to the objects that the constructor will create. For example, the following is a constructor function that creates a circle:

function Circle(radius) {
	this.radius = radius;
	this.area = Math.PI * radius * radius;
}

Constructor functions are commonly used to fill in the methods of an object.

function Area() {
	this.circleArea = MAth.PI * radius * radius;
}

To use an object in a script, you must assign it to a variable. To create a new circle object with the radius 5, use the new operator to create the object and assign it to the local variable myCircle:

var myCircle = new Circle(5);

Note: Objects have the same scope as the variable to which they are assigned. See Scoping a variable.


 
Creating inheritance

All functions have a prototype property that is created automatically when the function is defined. When you use a constructor function to create a new object, all the properties and methods of the constructor's prototype property become properties and methods of the __proto__ property of the new object. The prototype property indicates the default property values for objects created with that function. Passing values using the __proto__ and prototype properties is called inheritance.

Inheritance proceeds according to a definite hierarchy. When you call an object's property or method, ActionScript looks at the object to see if such an element exists. If it doesn't exist, ActionScript looks at the object's __proto__ property for the information (object.__proto__). If the called property is not a property of the object's __proto__ object, ActionScript looks at object.__proto__.__proto__.

It's common practice to attach methods to an object by assigning them to the object's prototype property. The following steps describe how to define a sample method:

1 Define the constructor function Circle, as follows:
function Circle(radius) {
	this.radius = radius;
}
2 Define the area method of the Circle object. The area method will calculate the area of the circle. You can use a function literal to define the area method and set the area property of the circle's prototype object, as follows:
Circle.prototype.area = function () {
		return Math.PI * this.radius * this.radius;
		}
3 Create an instance of the Circle object, as follows:
var myCircle = new Circle(4);
4 Call the area method of the new myCircle object, as follows:
var myCircleArea = myCircle.area()
ActionScript searches the myCircle object for the area method. Since the object doesn't have an area method, its prototype object Circle.prototype is searched for the area method. ActionScript finds it and calls it.

You can also attach a method to an object by attaching the method to every individual instance of the object, as in this example:

function Circle(radius) {
	this.radius = radius;
	this.area = function() {
		return Math.PI * this.radius * this.radius;
	}
}

This technique is not recommended. Using the prototype object is more efficient, because only one definition of area is necessary, and that definition is automatically copied into all instances created by the Circle function.

The prototype property is supported by Flash Player version 5 and later. For more information, see ActionScript dictionary: Overview.