Then how does it support inheritance, you might ask. And if it does not support inheritance, dare you call it an object oriented programming language, you might argue.
But, why would you do that?
When an object A is used as the prototype to create another object B, all the functions and members of object A auto-magically descends to object B.
To see the above statement in practice, let’s see an example:
Now, let’s create another function object called “Employee”. It takes in a parameter called “name” and just calls the constructor of “Person”. In case you are familiar with Java or C++, it is like calling super class’s constructor.
What “call” does is- it sets the name property for the specified “this” pointer. So basically we are setting “name” property of “Employee” and not the “Person” object.
Now let’s use “Person” as a prototype for our new object “Employee”. Thus we link “Person” object to the “Employee” object.
Now, whenever you create a new “Employee” object- its prototype will be set to “Person”.
And as mentioned in the beginning of the article, every Employee object will inherit members and methods of its prototype. Thus, “bob” being “Employee” object can set its “name” property and call “printName” method which are members declared in “Person” object.
The above magic is what is called as the Prototypal inheritance!
If you want to know more on how Prototypal inheritance is different and better than Classical inheritance- check out this post.
Drop me a line to let me know what do you think of this article, or if I missed anything.