User Guide
140 Object-Oriented Programming in ActionScript
To create a class in ActionScript 1.0, you define a constructor function for that class. In
ActionScript, functions are actual objects, not just abstract definitions. The constructor
function that you create serves as the prototypical object for instances of that class. The
following code creates a class named Shape and defines one property named
visible that is
set to
true by default:
// base class
function Shape() {}
// Create a property named visible.
Shape.prototype.visible = true;
This constructor function defines a Shape class that you can instantiate with the new operator,
as follows:
myShape = new Shape();
Just as the Shape constructor function object serves as the prototype for instances of the Shape
class, it can also serve as the prototype for subclasses of Shape—that is, other classes that
extend the Shape class.
The creation of a class that is a subclass of the Shape class is a two-step process. First, create
the class by defining a constructor function for the class, as follows:
// child class
function Circle(id, radius)
{
this.id = id;
this.radius = radius;
}
Second, use the new operator to declare that the Shape class is the prototype for the Circle
class. By default, any class you create uses the Object class as its prototype, which means that
Circle.prototype currently contains a generic object (an instance of the Object class). To
specify that Circle’s prototype is Shape instead of Object, use the following code to change the
value of
Circle.prototype so that it contains a Shape object instead of a generic object:
// Make Circle a subclass of Shape.
Circle.prototype = new Shape();