Prototypal Inheritance In Javascript trips me up all the time!
Javascript has prototypal inheritance.
It is one of the few mainstream languages to have this kind of a method to inherit.
What is prototyping?
Prototyping means to copy the base for creation and then extend the base to the desired features. Then what is classical inheritance? It is to have a fixed frame for an object in the form of a class. This class is duplicated into an object form. For objects with more features, one must first extend the class and then create objects out of the extended class.
In a language where prototypal inheritance is practiced, it generally encourages changing the object properties during run time and we know for sure this happens in Javascript
This also means that Objects in Javascript are mutable.
So to be clear
Classical Inheritance —> define classes and create an instance of these classes
Prototypal Inheritance —> define objects and create new objects from old objects
Code Reuse
In a classical language, it is very clear how code reuse happens. A class is used multiple times to create instances. By extending a class, we make sure the instances of the extended class have the properties of the parent class.
In a prototypal language, it is a bit hazy as to how the child object gets to use the properties defined in the parent. Usually, the language provides a construct/default property on the object to connect it to its parent. This is called delegation.
In Javascript specifically, one is allowed to create Objects ex-nihilio(out of nothing). When this is done, such an object is internally prototyped from a universal object call Object
. This base object consists of some basic functions such as toString()
Now how is delegation practiced in javascript? A property called prototype
is given to each of the object(except the object literals). This provides the option to chain objects in a heirarchy.
Lets write some code to demonstrate this.
This is the basic working of the prototype object.
Lets look deeper.
This seems like correct behaviour. You dont want the child to be able to upset the parent.
This shows that the value in the object always overrides the value in the prototype object. Thus the priority of a variable decreases as we go up the prototype chain.
The idea of the prototype is that in the above example, all instances of myChild
reuse the prototype objectmyObj
. This means that new instance of myObj
is not created. All instances of myChild
will share one instance of myObj
Creating a new Object from another Object.
The newer browsers allow us to use Object.create(anotherObj)
to create objects while inheriting from another object. For eg
What is the constructor?
Lets consider this -
Summary
- Javascript reuses code by Delegation of the prototype to all children.
- Change in prototype affects already created children.
- Children can always override parents properties.
- Set the constructor to know your roots!
blog comments powered by Disqus