About constructor functions

The constructor of a class is a special function that is called automatically when you create an instance of a class by using the new keyword (such as, var my_xml:XML = new XML();). The constructor function has the same name as the class that contains it. For example, a custom Person class that you create would contain the following constructor function:

public function Person(speed:Number) {
    Person.numPeople++;
    this._speed = speed;
}

Then you could create a new instance by using:

var myPerson:Person = new Person();

NOTE

 

If you do not explicitly declare a constructor function in your class file--that is, if you don't create a function whose name matches that of the class--the compiler automatically creates an empty constructor function for you.

A class can contain only one constructor function; overloaded constructor functions are not allowed in ActionScript 2.0. Also, a constructor function cannot have a return type. For more information on writing constructor functions in class files, Writing the constructor function.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000753.html