You are messing with the SubClass
‘s prototype with the SuperClass
‘s object, in this line
SubClass.prototype = new SuperClass();
the child’s prototype should depend on the Parent’s prototype. So, you can inherit like this
SubClass.prototype = Object.create(SuperClass.prototype);
Also, it is quite normal to change the constructor to the actual function, like this
SubClass.prototype.constructor = SubClass;
To keep your implementation generic, you can use Object.getPrototypeOf
, to get the parent prototype in the inheritance chain and then invoke printInfo
, like this
SubClass.prototype.printInfo = function() {
Object.getPrototypeOf(SubClass.prototype).printInfo(this);
};
Since, info
is defined in the SubClass
yet, it will print undefined
. You might also want to call the parent’t constructor, like this
var SubClass = function() {
SuperClass.call(this);
};
Note: You are creating global variables, by omitting var
keyword before SuperClass
and SubClass
.