2016-04-08 123 views
1

我有一些類,相關的「父子」(構造函數):擴展繼承原型方法在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

+0

sayHello()不是抽象類A的方法。它繼承自其原型 –

+0

有時您使用'inherit'並且有時使用原型。爲什麼?是否有任何要求? –

+0

@RIYAJKHAN我需要從祖先類原型中獲取(複製)所有可能的方法時使用inherit()。我使用了原型(在這裏我猜?'SuperClass.prototype.sayHello「')只是爲了快速輸入新方法。 –

回答

1

嘗試訪問初始類:

extend(ClassA.prototype, { 
    sayHello: function() { 
     SuperClass.prototype.sayHello.call(this); 
     this.sing(); 
    }, 

    sing: function() { 
     alert('Lalala'); 
    } 
}); 

或只是存儲當前sayHello()方法:

var initialSayHello = ClassA.prototype.sayHello; 
extend(ClassA.prototype, { 
    sayHello: function() { 
     initialSayHello.call(this); 
     this.sing(); 
    }, 

    sing: function() { 
     alert('Lalala'); 
    } 
}); 

你需要有參照原sayHello()方法。


Uncaught RangeError: Maximum call stack size exceeded

它被拋出,因爲你確實有一個無限遞歸調用該方法的方法本身。

+0

Dmitri,謝謝你的幫助。我看到你建議硬編碼鏈接到祖先原型方法,如果沒有在第一種情況下明確寫入類名,我能解決同樣的問題嗎? –

+0

@Jeepegg您需要以某種方式訪問​​原型,並且使用構造函數是可以的,但它會有些奇怪:'this .__ proto__ .__原__。sayHello.call(這)'。 –