AS1 OOP: Custom Object Classes
by senocular
The Constructor Property
Every time an instance is made, it receives not only what
properties you define for it, but also, it automatically
gets defined for it another hidden property called
constructor. As its name implies, it represents the
constructor function used to create that instance. Every
instance of a class, no matter if its an instance of Fish,
an array or even a movieclip, has a constructor property
which references the constructor function used to make that
instance (or at least the constructor function associated
with the instance as movieclips aren't technically created
exclusively with the MovieClip constructor). See the
following example.
- Folder = function(contents){
- this.contents = contents;
- };
- myDocuments = new Folder("untitled.fla");
- trace(myDocuments.constructor == Folder); // traces
true
- myPictures = new
myDocuments.constructor("clipboard.bmp");
- trace(myPictures.constructor == Folder); // traces
true
Since myDocuments is a Folder instance, its constructor
property references the Folder constructor function. With
that, as you can see, it can even be used to create another
instance of its same type. The myPictures object was done in
such a manner.