JSFiddle - React, Tailwind, and code Playground

by deepak sisodiya

JavaScript

// implementation of inheritance in javascript

var alert = function (str) {
    var st = document.createTextNode(str);
    var p = document.createElement('p');
    p.appendChild(st);
    document.querySelector('body').appendChild(p);
}

// define the Person Class
function Person() {}
Person.prototype.walk = function(){
  alert ('I am walking!');
}

// define the Student class
function Student() {
  // Call the parent constructor
  Person.call(this);
};

// inherit Person
Student.prototype = Object.create(Person.prototype);

// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;

Student.prototype.sayHello = function(){
  alert('hi, I am a student');
}

var student1 = new Student();
student1.sayHello();// call the derived class method
student1.walk();   // call base class method

// check inheritance
alert(student1 instanceof Person); // true 
alert(student1 instanceof Student); // true