Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In object-oriented programming, a class B can extend or subclass another class A. We say that A is the superclass and B is the subclass. Instances of B inherit all the instance methods of A. The class B can define its own instance methods, some of which may override methods of the same name defined by class A.
The key to creating subclasses in JavaScript is proper
initialization of the prototype object. If an object O is an instance of a
class B and B is a subclass of A, then O must also inherit properties from
A. We arrange this by ensuring that the prototype object of B inherits
from the prototype object of A. Using Object.create() (we could
also use the inherit() function from
Example 5-1), we write: