比方說,我有這樣的代碼(這是一個例子,簡化):更改「這個」只針對特定的函數調用
// A Thing that expands other thing can use it's origin Thing's "init".
ThingThatExpands.super = function() {
this.__originThing.init.apply(this, arguments);
}
// I make a Car Thing and give a color and a speed.
var Car = Thing("Toy Car", {
init: function(color, speed) {
this.color = color;
this.speed = speed;
}
});
// Then I make a Racing Car, which inherits the color and speed properties from it's origin
// (that's "Car", of course), but also has a cool sound!
var RacingCar = ThingThatExpands(Car, "Toy Police Car", {
init: function() {
this.super("#00F", 180);
this.sound = "Wee-ooh! Wee-ooh!"
}
})
現在,由於「RacingCar」是「汽車」的「孩子」 ,它有它的屬性和一個很好的「超級」功能,可以用來調用「汽車的」初始化。
好的。問題是:由於「超級」功能給「孩子」的東西作爲「這個」,它會改變「孩子」的屬性,對吧?好的,那就是我們需要的。 但是......這也意味着「超級」將被稱爲「孩子」而不是「起源」的事情,這不應該發生(並且不會工作)。
我可以以某種方式「保護」「this.super」呼叫嗎?
什麼是事情?這是來自圖書館還是ThingThatExpands? – thelastshadow
我認爲聲音應該是'this.sound =「Vrooom!Vroooom!」':) – ochi
這是一些僞代碼,但我試圖用我的OOP lib修復一個問題。 –