如果重写了JavaScript中的原型方法,则可以调用该基础方法吗?
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 } };
我不明白您到底想做什么,但是通常按照以下方式完成特定于对象的行为:
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...*/); }