markvpc markvpc - 3 months ago 10
Javascript Question

Calling base method using JavaScript prototype

Is it possible to call the base method from a prototype method in JavaScript if it's been overridden?

MyClass = function(name){
this.name = name;
this.do = function() {
//do somthing
}
};

MyClass.prototype.do = function() {
if (this.name === 'something') {
//do something new
} else {
//CALL BASE METHOD
}
};

Answer

I did not understand what exactly you're trying to do, but normally implementing object-specific behaviour is done along these lines:

function MyClass(name) {
    this.name = name;
}

MyClass.prototype.doStuff = function() {
    // generic behaviour
}

var myObj = new MyClass('foo');

var myObjSpecial = new MyClass('bar');
myObjSpecial.doStuff = function() {
    // do specialised stuff
    // how to call the generic implementation:
    MyClass.prototype.doStuff.call(this /*, args...*/);
}