Inheritance via constructor and prototype
Combining the use of parent class constructors for initialization and prototypes for inheritance provides benefits with regard to inheritance, initialization and method overridding.
Advantages
-
Clearly identifies parent class.
-
Easy initialization of properties inherited from parent class.
-
Clear inheritance chain available from instanceof
-
Easy to call overridden methods from parent class
Disadvantages
-
The initialization of the myChildClass prototype is not deferred in this most common use of prototype based inheritance. This required that the parent class be instantiated while the page was loading but before the
DIV
used to contain messages was fully ready. -
Initialization of prototype from a parent class can lead to dependencies in the child class on the arguments used to construct the parent class instance used as a prototype. For example, an immediate property in the parent class which becomes a prototype property in the child class.
As you can see in the example, the
name
property ofmyChildClass
was prototyped to the value defined in the call to create the instance ofmyParentClass
used as myChildClass's prototype.