我有一些類,相關的「父子」(構造函數):擴展繼承原型方法在Javascript
// Abstract root SuperClass
function SuperClass() {
this.CLASS_ID = "SUPER-CLASS";
throw new Error('Failed to instantiate abstract class' + this.CLASS_ID);
}
SuperClass.prototype.sayHello = function() {
alert('Hello, world!');
};
// Absctract ClassA inherits from SuperClass
// inherit() and extend() are generic functions from the David Flanagan`s
// brilliand book "Definitive Guide"
function ClassA() {
this.CLASS_ID = "CLASS-A";
throw new Error('Failed to instantiate abstract class' + this.CLASS_ID);
}
ClassA.prototype = inherit(SuperClass.prototype);
// Concrete ClassB inherits from ClassA
function ClassB(initData) {
this.CLASS_ID = "CLASS-B";
}
ClassB.prototype = inherit(ClassA.prototype);
extend(ClassB.prototype, {
constructor: ClassB,
welcomeNewDay: function() {
this.sayHello();
this.doSomethingElse();
},
doSomethingElse: function(){
alert('Jumping');
}
});
var classB = new ClassB();
classB.welcomeNewDay();
我怎樣才能正確地擴展抽象ClassA
的方法.sayHello()
沒有超載呢?
I`ve試圖讓這種方式:
extend(ClassA.prototype, {
sayHello: function() {
this.__proto__.sayHello();
this.sing();
},
sing: function() {
alert('Lalala');
}
});
的問題是,.sing()
被調用3次而不是1
如果我嘗試:
this.__proto__.sayHello.call(this);
它拋出異常:
Uncaught RangeError: Maximum call stack size exceeded
sayHello()不是抽象類A的方法。它繼承自其原型 –
有時您使用'inherit'並且有時使用原型。爲什麼?是否有任何要求? –
@RIYAJKHAN我需要從祖先類原型中獲取(複製)所有可能的方法時使用inherit()。我使用了原型(在這裏我猜?'SuperClass.prototype.sayHello「')只是爲了快速輸入新方法。 –