I have a "SuperClass" with "info" as an instance variable. "SuperClass" has function "printInfo()". "printInfo()" needs to access instance variable "info". I want to create a "SubClass" which also has method "printInfo()". I want to call printInfo() of "SuperClass" from "printInfo()" of "SubClass".
SuperClass = function()
{
this.info = "I am superclass";
console.log("SuperClass:");
};
SuperClass.prototype.printInfo = function(that)
{
console.log("printing from superclass printInfo");
console.log(that.info);
};
SubClass = function(){};
SubClass.prototype = new SuperClass();
SubClass.prototype.printInfo = function()
{
console.log("calling superclass");
this.constructor.prototype.printInfo(this);
console.log("called superclass");
};
var sc = new SubClass();
sc.printInfo();
You can see that I am passing "that" as a parameter to printInfo. Without "that" parameter, "info" is printed as "undefined". Like in the following case, "this.info" is undefined when this function is called from object of "SubClass".
SuperClass.prototype.printInfo = function()
{
console.log("printing from superclass printInfo");
console.log(this.info);
};
What is the proper way to override and invoke methods of superclass in javascript, enabling functions to access instance variables of the class?