Changing the Constructor's Prototype Object
This section provides a quick description of what is the default prototype object and how to change it. A tutorial example is provided to create your own prototype object and replace the default prototype object.
In the previous section, we saw that an object is automatically created and assigned to the constructor's "prototype" property when a constructor function is defined.
But if you don't like the object created by the system, you can create an object yourself and assign it to the "prototype" property.
To show you how to replace the default "prototype" object, I wrote the following tutorial example to allow "myBook" to inherit the "year" property stored in the prototype object created by myself:
The output of this tutorial example confirms that the default "prototype" object has been replaced by my own object called "myPrototype":
The diagram below shows what happened during the execution of this tutorial example.
The default object originally assigned to "Book.prototype" is grayed in the diagram.
Table of Contents