Creating a custom object in ActionScript 1

Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.

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 it this as a hidden parameter. For example, the following 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 the new instance a variable name. 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.