In Javascript I have a child object that inherits from a base object. I am trying to call a base class function from within a child object but I am getting an error saying that this function doesn’t exist.
Can you tell me how I call a base class function from within a child class?
function BaseClass()
{
this.render = function()
{
alert("BaseClass::render()");
}
}
function ChildClass()
{
this.render = function()
{
alert("BaseClass::render()");
// Now to CALL the base class render() function
this.baseRender = BaseClass.prototype.render;
this.baseRender(); // I get the error: "The function this.BaseRender() does not exist"
}
}
ChildClass.prototype = new BaseClass(); // inherit from BaseClass
ChildClass.prototype.constructor = ChildClass; // put the correct constructor reference back (not essential)
EDIT: OK, either I somehow didn’t see the last two lines of your code or you did a quick edit after I started writing my post.
You don’t have a child class there, you have two unrelated classes.Also the
render()method onBaseClassis currently not part of its prototype, it is assigned to each instance that is created withnew BaseClass(). Which meansBaseClass.prototype.renderis undefined.(And you’ve changed case from lower- to upper-case “b” in between assigningthis.baseRenderand trying to callthis.BaseRender(), but I assume that’s probably just a typo.)I think the simplest change to do what you are trying to do is this:
And remember that JavaScript doesn’t really have “classes” as such.