Advanced JavaScript: Implement Inheritance in JavaScript

Introduction

 
This is the "Advanced JavaScript" article series. In this series, we are learning a few advanced and interesting concepts of JavaScript. So far we have explained various important concepts of this programming language. You can get them here.
In this article, we will learn one more OOP concept of JavaScript called inheritance. As we have explained, JavaScript is an Object Oriented Programming language but is class-less. In other words, there is no concept of a class in JavaScript but we can implement a class by functions and objects. So, let's see how to implement the inheritance concept in JavaScript. 
 

Inheritance in JavaScript

 
Inheritance is the way to create a class as a specialized version of one or more classes. The specialized class is called the superclass or base class and the class that gets properties and functions from a specialized class is called a subclass. In modern browsers, we can also use Object.create to implement inheritance. Have a look at the following code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>    
  2. <!DOCTYPE html>    
  3. <html xmlns="http://www.w3.org/1999/xhtml">    
  4. <head runat="server">    
  5. </head>    
  6. <body>    
  7.     <form id="form1" runat="server">    
  8.         <script>           
  9.             function person(name) {    
  10.                 this.name = name;    
  11.             }     
  12.             person.prototype.SayName = function () {    
  13.                 alert("I am " + this.name);    
  14.             }     
  15.             function student(name) {    
  16.                 this.name = name;    
  17.             }     
  18.             //Inherit the property of parent class    
  19.             student.prototype = person.prototype;    
  20.             var s = new student('Sourav Kayal');    
  21.             s.SayName();     
  22.         </script>    
  23.     </form>    
  24. </body>    
  25. </html>   
    Let's try to understand it. As we said earlier, there is no class in JavaScript. And if we want to implement a class we need to use a function. Here we will inherit one function from another. At first we are defining the following function:
    1. function person(name) {    
    2.     this.name = name;    
    3. }   
      The function contains only one property called "name". When we create an object of this class we need to pass a name argument to it.
       
      Then, we are setting a property into it. The property is nothing but the elements that can be inherited by another class. The function name is SayHello and it will be inherited by the next class.
      1. person.prototype.SayName = function () {    
      2.     alert("I am " + this.name);    
      3. }   
        Now, we are defining the student class and will inherit the person's class for it. Here is the implementation of the student class. It also contains a name property.
        1. function student(name) {    
        2.     this.name = name;    
        3. }   
          Now, we have defined both a base and a derived class. It's time to inherit one from another. We need to set one class property to another to inherit the common property. This is code to share the property.
          1. //Inherit the property of parent class    
          2. student.prototype = person.prototype; 
            We will now create an object of the student class by passing the student name and then we will call the SayName() function to print the student name.
            1. var s = new student('Sourav Kayal');    
            2. s.SayName();   
              Here is a sample output:
               
              inheritance in JavaScript
               
              The code is calling the SayName() function defined in the Person class, not in the student class.
               
              This is the example of single inheritance. We will now try to implement multi-level inheritance. Have a look at the following code.
              1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>    
              2. <!DOCTYPE html>    
              3. <html xmlns="http://www.w3.org/1999/xhtml">    
              4. <head runat="server">    
              5. </head>    
              6. <body>    
              7.     <form id="form1" runat="server">    
              8.         <script>           
              9.             function father(name) {    
              10.                 this.name = name;    
              11.             }     
              12.             father.prototype.SayName = function () {    
              13.                 alert("Name is:- " + this.name);    
              14.             }     
              15.             function mother(name) {    
              16.                 this.name = name;    
              17.             }    
              18.             function son(name) {    
              19.                 this.name = name;    
              20.             }    
              21.             mother.prototype = father.prototype;    
              22.             son.prototype = mother.prototype;                
              23.             var s = new son('Sourav Kayal');    
              24.             s.SayName();     
              25.         </script>    
              26.     </form>    
              27. </body>    
              28. </html>   
                The code above is very similar to the first example. We are creating an object of the son class and using the SayHello() function of the father class.
                 
                inheritance in JavaScript
                 

                Conclusion

                 
                In this article, we saw how to implement the inheritance concept in JavaScript. Follow this series to understand more of the concepts of JavaScript.