Monday, February 26, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  30] [ 6]  / answers: 1 / hits: 160935  / 15 Years ago, wed, february 18, 2009, 12:00:00

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
}
};

More From » prototype

 Answers
51

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...*/);
}

[#99957] Tuesday, February 10, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
tomas questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
;