Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Object-Oriented Programming with ActionScript 1.0 > Creating a custom object in ActionScript 1.0 | |||
|
NOTE |
Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes. |
To create a custom object, you define a constructor function. A constructor function is always given the same name as the type of object it creates. You can use the keyword this inside the body of the constructor function to refer to the object that the constructor creates; when you call a constructor function, Flash passes this to the function as a hidden parameter. For example, the following code is a constructor function that creates a circle with the property radius:
function Circle(radius) {
this.radius = radius;
}
After you define the constructor function, you must create an instance of the object. Use the new operator before the name of the constructor function, and assign a variable name to the new instance. For example, the following code uses the new operator to create a Circle object with a radius of 5 and assigns it to the variable myCircle:
myCircle = new Circle(5);
|
NOTE |
An object has the same scope as the variable to which it is assigned. |
|
|
|
|