| |
|
||||
|
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.
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
|