The standard pseudo-classical class will have properties that are set in the constructor and functions that are associated with the constructor’s prototype. When I refer to a class’s prototype I mean the class constructor’s prototype. Any potential subclass needs access to both the properties and the functions of that class. Getting the superclass’s properties is straight forward. The superclass’s constructor can be reused with the call method. By passing the subclass’s ‘this’ binding to the call method, instances of the subclass will inherit all the properties of the superclass. Getting the superclass’s functions from its prototype isn’t as intuitive.
The first way that comes to my mind is to set the subclass’s prototype to the superclass’s prototype. What this does is make the subclass and superclass have the same prototype. Any new functions I add to the subclass would also be added to the superclass. Part of subclassing is that the subclass receives new properties and functions that are not in the superclass.
Object.create causes the relationship between the subclass’s prototype and constructor to be lost. To reestablish this connection, the subclass prototype’s constructor can be set equal to the subclass’s constructor. This completes subclassing for the pseudo-classical pattern, but I wanted to know more about how Object.create forms the delegation relationship between the subclass’s prototype and superclass’s prototype.
The prototype chain can be used to link multiple classes together. For example, given three classes A, B, and C, I want C’s prototype to have access to the functions on A’s and B’s prototypes. To accomplish this I can set up a prototype chain between A, B, and C. First I would use Object.create to set B’s prototype to a new object with A’s prototype as its prototype. Then I would use Object.create again to set C’s prototype to a new object with B’s prototype as its prototype. After setting up the prototype chain I would want to make sure I reset B and C prototypes’ constructors to B and C respectively. Now if I call a function with an instance of C that is on B’s prototype, but not C’s prototype, a look up will happen to B’s prototype. If a function I want to call is on A’s prototype and not C’s or B’s prototypes, then a look up will first happen to B’s prototype followed by a look up to A’s prototype.