我有以下代码:
function Rune(){
this.subSpells = [];
}
function Modifier(){
this.type = "modifier";
}
Modifier.prototype = new Rune();
function RuneFactory(effect, inheritsFrom, initialValue){
var toReturn = function(){};
toReturn.prototype = new inheritsFrom();
toReturn.prototype.subSpells[effect] = initialValue;
return toReturn;
}
Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);
x = new Duration();
y = new Quicken();x.subSpells.duration和x.subSpells.quicken都等于1。与y相同。我希望x.subSpells.quicken和y.subSpells.duration都是未定义的。
如果我在持续时间和快速定义中做了以下操作,我就得到了我想要的行为。
Duration = RuneFactory("duration", Rune, 1);
Quicken = RuneFactory("quicken", Rune, 1);我认为双重继承有问题。有人能告诉我如何更改我的RuneFactory代码,使它能够使用双重继承和/或解释什么是坏的吗?如果可能的话,我想避免使用框架。
谢谢!
发布于 2014-01-02 10:16:01
在修饰符函数Rune.apply(this, arguments);中添加下面一行,因为subSpells是在prototype级别定义的,所以Rune的所有实例都指向同一个对象。
并将subSpells的类型从数组更改为对象。
正如所提到的,如果使用寄生组合继承模式,会更好。
“我认为双重继承有问题”--不!
首先,不存在双重继承,您与修饰符构造函数有相同的问题,您的实例引用相同的subSpells对象(在您的情况下是数组)。阅读关于原始值与参考值或参考和价值的文章是关于.NET的,但是编程中的基本原则是相同的)。
看看JavaScript .prototype是如何工作的?
看看http://jsfiddle.net/L7dVL/
function inheritPrototype(subType, superType) {
var prototype = Object.create(superType.prototype, {
constructor: {
value: subType,
enumerable: true
}
});
subType.prototype = prototype;
}
function Rune() {
this.subSpells = {};
}
function Modifier() {
Rune.apply(this, arguments);
this.type = "modifier";
}
inheritPrototype(Modifier, Rune);
function RuneFactory(effect, inheritsFrom, initialValue) {
function toReturn() {
inheritsFrom.apply(this, arguments); // you should bind this
this.subSpells[effect] = initialValue;
}
inheritPrototype(toReturn, inheritsFrom);
return toReturn;
}
Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);
x = new Duration();
y = new Quicken();
console.log(x.subSpells.duration); // 1
console.log(x.subSpells.quicken); // undefined
console.log(y.subSpells.duration); // undefined
console.log(y.subSpells.quicken); // 1发布于 2014-01-02 10:15:00
在JavaScript中继承的正确方法是:
function Parent(arg1, arg2) {
// Do something
}
function Child(arg1, arg2) {
Parent.call(this, arg1, arg2); // or Parent.apply(this, arguments)
}
Child.prototype = Object.create(Parent.prototype, {
constructor: {
value: Child
}
});所以你需要这样的东西:
function Rune(){
this.subSpells = {};
}
function Modifier(){
Rune.apply(this, arguments);
this.type = "modifier";
}
Modifier.prototype = Object.create(Rune.prototype, {
constructor: {
value: Modifier
}
});
function RuneFactory(effect, inheritsFrom, initialValue){
var toReturn = function(){};
toReturn.prototype = Object.create(inheritsFrom.prototype, {
constructor: {
value: Modifier
}
});
toReturn.prototype.subSpells = {};
toReturn.prototype.subSpells[effect] = initialValue;
return toReturn;
}
Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);
x = new Duration();
y = new Quicken();注意:在您的问题中,.subSpells是一个数组,但是您指定了一个成员,因为它将是一个对象!
https://stackoverflow.com/questions/20880314
复制相似问题