I am trying to learn inheritance. If I am defining my method superPrint() in SuperClass, I am unable to execute using the child instance
eg: new ChildClass().superPrint(); //Throws error fn not available
/*Parent Class*/
var SuperClass= function(){
this.name = '';
this.superPrint = function(){console.log('Doesnt Work');};
}
/*Child Class*/
var ChildClass= function(){
this.print=function(){
console.log('Always works');
}
}
/*Child method inheriting from the parent method*/
ChildClass.prototype = Object.create(SuperClass.prototype);
/*Instantiating the child method*/
var obj = new ChildClass();
obj.print();//This works
obj.superPrint();//This gives error *******
But if the function superPrint() is defined using prototype it works. Why?
(called using new ChildClass().workingPrint(); now it will work)
/*Parent Class*/
var SuperClass= function(){
this.name = '';
}
SuperClass.prototype.workingPrint = function(){console.log('This Works');};