Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "inheritance" in Javascript?

Tags:

Can anyone explain to me with simple words the meaning of "inheritance" in JavaScript?

like image 501
Dima Avatar asked Feb 17 '11 09:02

Dima


People also ask

What is inheritance explain?

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.

Is there inheritance in JavaScript?

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.

What is inheritance in JavaScript ES6?

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.


2 Answers

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 functions 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 
like image 154
David Tang Avatar answered Oct 03 '22 00:10

David Tang


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

like image 30
Saurabh Gokhale Avatar answered Oct 02 '22 23:10

Saurabh Gokhale