Can anyone explain to me with simple words the meaning of "inheritance" in JavaScript?
Inheritance is the process by which genetic information is passed on from parent to child. This is why members of the same family tend to have similar characteristics.
Inheritance is an important concept in object oriented programming. In the classical inheritance, methods from base class get copied into derived class. In JavaScript, inheritance is supported by using prototype object.
The ES6 JavaScript supports Object-Oriented programming components such as Object, Class and Methods. Further in Classes we can implement inheritance to make child inherits all methods of Parent Class. This can be done using the extends and super keywords. We use the extends keyword to implement the inheritance in ES6.
In simple terms, inheritance is the concept of one thing gaining the properties or behaviours of something else. To say A inherits from B, is saying that A is a type of B. A Bird inherits from Animal because a Bird is a type of Animal - it can do the same things, but a little more (or differently)!
In JavaScript, this relationship is a little complicated to specify, but bear with the syntax. You must use a special object called prototype
which assigns properties to a type such as Animal. Only function
s have a prototype
, which is why you must create a function first:
function Animal() {}; // This is the Animal *Type* Animal.prototype.eat = function () { alert("All animals can eat!"); };
Now to create a type that inherits from Animal, you also use the prototype
object, but this time the one belonging to the other type, e.g. Bird:
function Bird() {}; // Declaring a Bird *Type* Bird.prototype = new Animal(); // Birds inherit from Animal Bird.prototype.fly = function() { alert("Birds are special, they can fly!"); };
The effect of this is that any Birds you create (called an instance of Bird) all have the properties of Animals, but they also have the additional .fly()
:
var aBird = new Bird(); // Create an instance of the Bird Type aBird.eat(); // It should alert, so the inheritance worked aBird.fly(); // Important part of inheritance, Bird is also different to Animal var anAnimal = new Animal(); // Let's check an instance of Animal now anAnimal.eat(); // Alerts, no problem here anAnimal.fly(); // Error will occur, since only Birds have fly() in its prototype
A nice explanation by Robert on Inheritance in Javascript here
The way inheritance works in JavaScript is that it is prototype
, instead of class-based
.
For e.g
function Being () { this.living = true; } Being.prototype.breathes = function () { return true;
An object which inherits Being
Robert.prototype = new Being; function Robert () { this.blogs = true; } Robert.prototype.getsBored = function () { return "You betcha"; };
So, basically, if we create a new instance of the Robert object and call some of its methods, the result would be:
// Create an instance of the Robert object var me = new Robert(); /* Returns "You betcha" as it's a method belonging to the Robert object */ me.getsBored(); /* Returns true. Since the Robert object doesn't have a breathes method of its own, it goes back in the prototype chain to its parent object, Being, and finds the method there */ me.breathes();
Also a good read of JavaScript inheritance : JavaScript Inheritance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With