In this section, we will talk about another way, via which we can inherit prototypes. Here, I’ll be using Object.Create to use the same. In the last section, we have seen function constructor pattern to create objects from the blueprint. With that object.create() method, things work bit differently. In this case, we first define an object which will act as a prototype and then create a new object based on that very prototype.
Below, I have pasted snippet for the same.
You can also see that calcAge is inherited here via prototype. However, there is other way of creating object as well like passing these values as 2nd argument.
With the above change in place, When I see the output, it will come like
Same thing right, no change in output. Above is just syntactic sugar around the same. It basically depends on developer’s choice, which way they prefer to create. One of the biggest benefit of Object.create, that it allows us to create really complex inheritance rather than function constructor. Therefore, function constructor and Object.create are two ways of creating objects. Thanks for joining. In coming section, we will delve further and see other advanced stuffs.
263 total views, 1 views today