Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
 

Constructor

The term constructor means, "the thing that constructs objects". We know that all objects are created from classes - so a constructor must then be YAFWF (yet-another-fancy-word-for) a class. Well, sort of. Just as a class is a specific type of function (in Actionscript only!), a constructor is a specific part of the class. It is the part that is involved in 'constructing' objects, as one would guess. We know that objects are constructed using the activation object and arguments. So then a constructor seems most like the activation object, and yes, that is pretty much what a constructor is. The confusion comes when we remember that an activation object does not exist until you call it, and it is destroyed when it finishes - not exactly a friend you would add to your address book. In fact the only way to 'activate' this activation object is to use the property it is assigned to, which is the object that names the class. This works out well, because the arguments are sort of sandwiched between these two objects, and they are so tightly bound together that they are often thought of as a single object anyway.

Why is it so important to get such a precise meaning of constructor? Because it is another one of those undocumented Actionscript keywords that can be quite handy. Every instance has a constructor property, and it points to the class that made it. To be exact then, it points to the object that names its class. When you use the word constructor from the point of view of an instance (as in rover's constructor), it suggests the class that created the instance. When talking about parts of the class, the constructor would suggest the activation object, along with the arguments and the name object. It shouldn't really suggest the prototype, or properties that have been assigned to the class name object (like Dog.count), because these are not directly involved in 'constructing' an object. You may think this is splitting hairs on a camel. Yeah, probably it is. In most other OO languages, the constructor and the name of the class are very different things. In Actionscript, they are still different things but they are rolled up together into a ball, leaving only their meanings to separate them. This is why it is important to have precise technical definitions for these words, so formally:

  • Class - the whole shebang.
  • Constructor - the part that makes new thingies.

We previously alluded to how instances can reference the class that created them, and yes, the constructor keyword is how this is done. It is always used from within an object, and most usefully used from within an instance you have created from your own class (though it is also very useful for poking around under the hood of Actionscript too!). Here are two ways you can 'find' the constructor from an instance.

Dog = function( )
{
  this.instNum = ++this.constructor.count;
}

// these next two properties aren't really part of the 'constructor'
Dog.count = 0; // used to count how many Dogs have been created
Dog.className = "Dog"; // used to remember the name of the class

rover  = new Dog( );
fido   = new Dog( );

trace( rover.instNum ); // 1 (first Dog created)
trace( fido.instNum );  // 2 (second Dog created)
trace( rover.constructor.className ); // Dog

Though it is not all that common to use the constructor property, it sometimes can be just the thing. The obvious advantage of accessing Dog's properties using instance.constructor.xxx rather than Dog.xxx is that if you ever change the name of the Dog class to Canine, you will not have to hunt through your code looking for where you specifically mentioned Dog. This makes your code much more portable as well, in case you should want to use it in a different class, or even different program.

Interestingly, a class's prototype does have a constructor, because it is, in fact, an object that has been created. When you define a class it immediately creates the name object, but it does not create an activation object (that only happens when it's called). It does create the prototype object, though. This also happens as soon as the class is defined. Where does the prototype's constructor point to? The class name object, just like an instance does. How would you check such a thing? In the above function you could just add:

trace( Dog.prototype.constructor.className ); // Dog

That is indeed a mouthful, but if you follow it you can see it simply says, 'Trace the className of the object that created (constructed) the Dog prototype'. OK, maybe it's still a mouthful. If you move that line up and down in the code, you can see that the prototype object is created as soon as the Dog class is defined - before any properties are set in the prototype.

The last weird thing to know is that the class name object (Dog) has no constructor property, even though it is an object that has been created. Function names do not have a constructor in Actionscript, even though they do in similar languages like Javascript. The reasons are complex, but boil down to Actionscript fitting nicely inside the 'tiny' 200K Flash player. Err, I think. Umm, let's quickly move on to the next section about methods. It is a meaty one.

Arguments < < Home > > Methods