How to "Achieve Inheritance" in JavaScript?
There are two ways
- Pseudo classical Inheritance
- 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!