我想知道,鑑於JavaScript的原型繼承的限制,是否有可能仿效其他OOP語言中所見的基於類的繼承。Javascript - 可以模擬經典的OOP繼承嗎?
我創建了一個超類和子類,如下所示:
//Parent
function Animal(i)
{
//Some constructor implementation
}
//Child
function Pig(i)
{
//Some additional constructor implementation
}
Pig.prototype = new Animal();
我可以保證的是,孩子這樣,我並不需要顯式調用,或者創建他們的方式繼承其父的功能孩子的對象?
Animal.prototype.eat = function()
{
console.log("eating...");
}
Pig.eat(); //No implementation of eat in the Pig object
我可以保證孩子繼承了所有除了自身的那些其父對象變量的不顯式調用這樣的父類的構造:
function Pig(i)
{
User.call(this, i);
//Some pig-like variables
}
基本上,我想創建在我的父類中的所有實現,只寫需要重寫的函數+子類中的任何附加函數。如果我想調用Pig.eat(),我希望它使用父函數,如果在子對象中不存在。在創建一個Pig對象時,我希望它能夠繼承父對象的變量以及它自己的變量。這可能嗎?
首先,你需要做一個'新Pig' *實例*之前,你可以調用像'eat'它的方法。並且你[不應該使用'new'來創建原型對象](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) – Bergi
不,就像在經典繼承中一樣,每次都需要顯式調用'super'(這裏:'Animal.call(this,i);')。當然,在JS中,你可以通過元編程在這裏獲得一些糖。 – Bergi
*「我可以確保孩子繼承父母的功能嗎?我不需要顯式調用它們,或者在子對象中創建它們?「*看起來你只需要瞭解[原型如何工作](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch5.md )。我鼓勵你接受JavaScript的原型繼承,並忘記了基於類的繼承。 –