Constructor Chaining

When we create a new class, we would rather not repeat the code from the constructors of all base classes. This would violate the DRY principle and would cause maintenance problems. To function like Java, we need a way to chain constructors together in the correct order. When we define a new class, we will create an array with references to a constructor for the new class and all base classes, beginning with the constructor for the most distant ancestor as element 0. We will refer to these as pseudo-constructors since they are not the constructor the interpreter will call when executing the new operator. We will generate the real constructor and its only task will be to call each pseudo-constructor. Figure 3 illustrates the changes we will make to the prototypal inheritance mechanism.


Figure 3 - Constructor Chaining

Our framework will automate all of the steps necessary to create a class constructor, create a prototype that chains to the superclass prototype, add our methods to the prototype, and create the constructor chain.

Next we will describe a universal base class that will call our constructor chain.