JavaScript Prototypes
Explore JavaScript prototypes to understand how object inheritance works in OOP. Learn to access and modify prototypes using __proto__ and methods such as Object.getPrototypeOf and Object.setPrototypeOf. This lesson helps you grasp how prototypes connect objects and influence property lookup.
We'll cover the following...
Background
We know about JavaScript objects and their mutation. We have assigned properties from one object to another. The question still remains. Is there a neater way of having all properties of another object?
Introduction to prototypes
All JavaScript objects have the property prototype. They go under the property name __proto__. The prototype of each object is assigned during creation and is itself an object. Look at the following code.
In the code above, each object is assigned to the three variables: vehicle, car, and driver (lines 1 to 3). We also print the __proto__ property for each variable, which is otherwise hidden when printing an object including an empty object (driver). The __proto__ property for each object can be seen as an empty object because the properties of the prototype object are ...