JS-ProtoType Inheritance
JS-ProtoType Inheritance
by ayyanarj
JavaScript
const parent = function() {
this.firstName = "Ayyanar";
}
const child = function() {
this.lastName = "Jayabalan";
}
child.prototype = new parent();
parentObj = new parent();
childObj = new child();
/*
console.log(childObj.firstName);
console.log(childObj.lastName);
console.log(parentObj.firstName);
console.log(parentObj.lastName);
*/
/* ES6 Way to doing inheritance*/
class ParentES6 {
constructor() {
this.firstName = "Ayyanar";
}
}
class ChildES6 extends ParentES6 {
constructor() {
super();
this.lastName = "Jayabalan";
}
}
parentObjES6 = new ParentES6();
childObjES6 = new ChildES6();
console.log(childObjES6.firstName);
console.log(childObjES6.lastName);
console.log(parentObjES6.firstName);
console.log(parentObjES6.lastName);