Beginning ActionScript 3.0 – Constructor


When we introduce the basic OOP concepts, we use an example to illustrate the OOP-style code(@see Beginning ActionScript 3.0 – Introduction to OOP). Inside the example, we use the new operator. Did you still remember the function of “new” operator?





We said the “new” operator calls the constructor. Now, we’ll talk about the constructor.

Constructor methods, sometimes simply called constructors, are functions that share the same name as the class in which they are defined. Any code that you include in a constructor method is executed whenever an instance of the class is created with the new keyword.

Go back to the statement.

  1. authorInstance = new Author();

Focus on the parentheses.

Due to our previous experience the appearance of parentheses always means the function call, isn’t it?

So, here, it looks like we call a method name Author, if we ignore the new operatorJ

Now, here comes the first part of constructor, the name.

The name of the constructor should the same with the class name.

Let’s try to add a constructor to the Author class.

Add the following code to the Author class.

  1. function Author(){
  2. trace(”The constructor of Author class”);
  3. }

It’s just a simple function with the same name of the class.

Run the code, and you’ll get:


The code runs smoothly.

Compare the method definition to other methods defined inside the class body, you can easily find out that:

Firstly, the constructor doesn’t have any attributes, such as internal or private. You can only use the “public” attribute optionally, but you can’t use any other attributes to modify the constructor. So, you can ignore the attribute, when you write the constructor.

Secondly, this method has no return declaration, a data type follow by the colon. The official document says, although it is legal to use the return statement inside a constructor, it is not permissible to return a value.


Consider the situation when we use the constructor.

  1. authorInstance = new Author();

After the constructor has been called, it should return a corresponding reference to the object, here is the Author type reference. Then the reference will be assign to the authorInstane here. So, if ActionScript allows you to return a value inside the constructor, it’ll get confused. We only expect an object reference to the corresponding class, not other things. And the return of object reference is better to handle by the compiler or runtime environment.

Please remember, don’t use the return statement inside the constructor.

Like other methods, the constructor can have parameters while you can’t call the constructor like you call other methods.

In general, the constructor will be call only once for an object, at the beginning of its lifecycle. So, in most cases, we put the initialization work inside the constructor. But, don’t try to initialize the static variables inside the constructor, because you can use the static variable before you initialize it in this situation.

We know the Author class is inherited from the Person class now.

Aha, next experiment now.

Add some code to the Person class, eh, just add a constructor to the Person class.

  1. function Person(){
  2. trace(”a new person was born”);
  3. }

Yeah, typical constructorJ

And run the test code again now, and you’ll get the following result.


The output means that when you call a constructor of a subclass, the constructor of the subclass will call its parent’s constructor. And this is won’t stop until it reaches the root of this inheritance hierarchy, the Object.

OK, all for this topic