inheritance and polymorphism
typeof operator applied to a function returns
“function”), and can therefore carry members, including properties that
reference other functions.
which is a reference to an Object.
The purpose of this property is to implement inheritance. It works as follows.
The technique is to set the
prototype of a
constructor to an object instance, which serves as the parent.
An object can then be created using operator
new with the first constructor. For each
public member of the parent, operator
new gives the created object a property that
refers to the parent’s member. An object created this way is called a
child of the parent object, and the child is said to
inherit the public members of the parent.
Any constructor may serve as a parent. For example:
There is nothing special about the parent constructor.
There are two non-obvious constructs required to achieve inheritance: setting up the inheritance relation, and initializing the members of the parent class inherited by the child class. There is more than one way to do it, but this is the best I’ve found.
The setting of the
prototype class variable to an instance
is derived from the other. (The constructor of the other class is obtained
automatically from the
constructor member of this Object.)
Note that the private members of the parent are not directly accessible to the child. That is as it should be.
Thanks to Pablo Krause for pointing out a bug in a previous version that had led me down a complicated road.
People argue about whether this is real object-oriented programming, but I personally like it, because often one wants to model things in the real world that also acquire and lose properties and behaviors over time.
In contrast to the situation in class-based languages, one must take steps to save the parent’s method before the child’s constructor replaces it, if the method of the parent is ever to be called by a child.