Mootools 类扩展了调用重写的父方法
Mootools class extends calling overridden parent method
我正在玩Mootools类继承等,我正在尝试使用.parent属性调用基类覆盖的方法(sry表示长代码片段)。
var app = this.app = {},
//ultimate base class
Animal = new Class({
initialize : function(param){
this.age = param.age;
this.name = param.name;
},
doStuff : function(param){
alert("animal doStuff");
}
}),
//herbivore functionality
HerbivoreBehaviour = new Class({
eat : function(){
alert('Plants are scrumptious');
}
}),
//carnivore functionality
CarnivoreBehaviour = new Class({
eat : function(){
alert('Meat tastes gooood');
}
})
//mammal base class
Mammal = new Class({
Extends : Animal,
initialize : function(param){
this.parent(param);
this.numberOfNipples = param.numberOfNipples;
},
doStuff : function(){
alert("mammal doStuff");
this.doStuff.parent();
}
}),
//reptile base class
Reptile = new Class({
Extends : Animal,
initialize : function(param){
this.parent(param);
this.numberOfScales = param.numberOfScales;
},
doStuff : function(){
alert('reptile doStuff');
this.doStuff.parent();
}
}),
//final top class animal->mammal-cow!
//and it's a herbivore
Cow = new Class({
Extends : Mammal,
Implements : HerbivoreBehaviour,
initialize : function(param){
this.parent(param);
this.isMooCrazy = param.isMooCrazy;
},
doStuff : function(){
alert('I am a cow');
this.doStuff.parent();
}
}),
//final top level class animal->reptile->mutantLizard!
//and it's a carnivore
MutantLizard = new Class({
Extends : Reptile,
Implements : CarnivoreBehaviour,
initialize : function(param){
this.parent(param);
this.isDestroyingEverything = param.isDestroyingEverything;
},
doStuff : function(){
alert('STOMP STOMP STOMP CRRRRRASH');
this.doStuff.parent();
}
});
app.run = function(){
var daisy = new Cow({
name : 'Daisy',
age : 2,
numberOfNipples : 6,
isMooCrazy : true
}),
godzilla = new MutantLizard({
name : 'Godzilla',
age : 1123,
numberOfScales : 123456,
isDestroyingEverthing : true
});
daisy.eat();
godzilla.eat();
daisy.doStuff();
godzilla.doStuff();
};
当前输出从 daisy.eat() 和 godzilla.eat() 是正确的,但在 daisy.doStuff() 的第一个打印语句之后执行停止,然后它不会调用 daisies 父类 doStuff() 方法,这正是我希望的。你能告诉我我怎么称呼这样的doStuff吗?
更新:
我设法通过 everlacnig this.doStuff.parent() 和 this.parent() 修复了他的调用序列。然而,这就提出了一个问题,即"这"指的是什么。我认为这将是类的实际实例,即。雏菊或哥斯拉,但是在doStuff方法中使用this.parent()似乎暗示,至少对我来说,"this"指的是它当前所在的实际函数对象,除非在函数中调用parent()方法会自动查找你所在的方法并调用它的第一个被覆盖的方法。
对。
基本上,一切都很好,这是个好消息。 唯一需要的改变是将this.doStuff.parent()
换成this.parent()
。
http://jsfiddle.net/dimitar/njQBN/
似乎工作正常。
做this.doStuff.parent()
一开始并不是很 DRY,它还引用了一个本地属性 - 在以相同方法扩展和实现其他属性的类的上下文中 - 同时,将具有......意想不到的结果。
相关文章:
- 如何重写一个方法,并且仍然能够在重写的方法中使用基方法
- 将其中一个异步方法重写为使用promise的方法
- 在类外重写Javascript方法,使用默认行为
- setStyleClass方法正在重写已定义的styleClass
- 重写jQuery插件方法
- 用函数重写reduce方法
- 重写JavaScript中的Object.create方法
- 如何重写这段代码,使执行不必从Javascript Promise.then()方法开始
- 对于EmberJs模型,为什么不建议重写“init”方法
- 重写javascript方法enable
- Mootools 类扩展了调用重写的父方法
- 如何重写特定数组的方法
- ES6 重写父方法
- 调用原型的重写方法,然后调用下一个原型的重写方法错误
- 在主干模型中重写 fetch() 方法
- 恢复“重写”窗口方法
- jQuery's的CSS方法重写:hover属性
- 方法重写不能正常工作
- JavaScript中的类/实例方法重写
- 我如何使用以下方法重写它...否则条款