【发布时间】:2015-11-24 19:59:20
【问题描述】:
假设我有这段代码(这是一个示例,已简化):
// 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”是“Car”的“子”,它有它的属性和一个很好的“超级”函数,可以用来调用“Car”的 init。
好的。问题是:既然“super”函数把“child”这个Thing作为“this”,它会改变“child”的属性,对吧?好的,这就是我们需要的。 但是......这也意味着“超级”将从“孩子”而不是“起源”事物中调用,这不应该发生(并且不会起作用)。
我能以某种方式“保护”“this.super”调用吗?
【问题讨论】:
-
什么是东西?是来自图书馆还是应该是 ThingThatExpands?
-
我觉得声音应该是
this.sound = "Vrooom! Vroooom!":) -
这是一些伪代码,但我正在尝试解决我的 OOP 库的问题。
标签: javascript class oop object call