Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 872 Bytes

README.md

File metadata and controls

39 lines (29 loc) · 872 Bytes

JavaScript Inheritance

How to "Achieve Inheritance" in JavaScript?

There are two ways

  1. Pseudo classical Inheritance
  2. Prototype Inheritance

Pseudo classical Inheritance is the most popular way. In this way, create a constructor function using the new operator and add the members function

Example as,

//This is a parent class.
var parent = {
        sayHi: function () {
            alert('Hi, I am parent!');
        },
        sayHiToWalk: function () {
            alert('Hi, I am parent! and going to walk!');
        }
};

//This is child class and the parent class is inherited in the child class.
var child = Object.create(parent);

child.sayHi = function () {
    alert('Hi, I am a child!');
};
<button type='submit' onclick='child.sayHi()'> click to oops</button>
The output is : Hi, I am a child!