1

I have an extended "class" extending a base "class" using prototypes. The problem I'm having is how do I call methods defined on the prototype of the base class (base methods) from the inheriting classes prototype method.

function Obj() {
    this.name;
}

Obj.prototype.baseMethod = function(usefulstuff) {
    alert(usefulstuff);
}

function extendedObj() {
    Obj.call(this);
}

extendedObj.prototype = new Obj();

extendedObj.prototype.constructor = extendedObj;

extendedObj.prototype.anotherMethod = function() {
    this.baseMethod(stuff);//gives this.baseMethod is not a function and direct call gives baseMethod is not defined
}

var a = new extendedObj();

a.anotherMethod();

Surely because the prototypes of both objects are the same and methods have only been added and because the prototype methods are public, this should be fine, unless this isn't how prototype chaining works?

1 Answer 1

1

You can affix a _super property referring to the super class. See here for more details. http://ejohn.org/blog/simple-javascript-inheritance/

Another approach is to directly call the method from the super class's prototype using Superclass.prototype.desiredMethod.call(this, args...). See here for more details. http://blog.salsify.com/engineering/super-methods-in-javascript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.