In the previous lecture, we learned about the classical oop model with classes and instances created from these classes. And remember that a class is like a blueprint which is a theoretical plan , and which we use to build many houses 🏠 in the real world. In the same way, the theoretical class can be used to create actual objects, which are called instances, which we can then use in our code. And this process of creating objects from a class is called instantiation.
Just note that this inheritance differs from the inheritance we discussed in the previous lecture. In the previous lecture, we had a class inheriting from another. But in this case, it's basically an instance inheriting from a class. Just keep that in mind 😉.
We can also say that objects delegate behavior (method ) to the linked prototype object. So besides prototypal inheritance, we also call this mechanism delegation. This, again, is another difference from the classical inheritance that we talked about in the previous lecture, where the behaviors (methods) are actually copied from the class to all instances.
We have actually already seen this mechanism in action many times before, but without knowing it was happening. For example, let's consider the below code:
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map((n) => n * 2);
Each time we use an array method like the above
map method, we are able to use it because of prototypal inheritance. You will also notice that when you go to MDN to check the documentation of any array method, what you will see there is that it is actually called
Array.prototype.map. The question now is, why is that relevant? Well,
This prototype object contains all the array methods, including
Array.prototype is the prototype object of the
numbers array, it means that
numbers is linked to that prototype. Hence, it has access to all the methods defined in the
Array.prototype object, just like the
map method. So, we can say our array inherits the
map method, or we can say that our array delegated the behavior of mapping to its prototype.
Constructor functions are a way of creating objects programmatically, using a function that will also set the new object's prototype. And this is how built-in objects like
This technique is basically the easiest and most straightforward way of linking an object to a prototype object. However, it is not as used as the previous two techniques, as we will see later.
To finish, one important thing to keep in mind is that the four principles of Object-Oriented Programming that we learned in the last lecture are still valid and important with prototypal inheritance.