我在 Mootools 中构建了一个类并扩展了两次,这样就有了祖 parent 、 parent 和 child 的关系:
var SomeClass1 = new Class({
initialize: function() {
// Code
},
doSomething: function() {
// Code
}
});
var SomeClass2 = new Class({
Extends: SomeClass1,
initialize: function() {
this.parent();
},
doSomething: function() {
this.parent();
// Some code I don't want to run from Class3
}
});
var SomeClass3 = new Class({
Extends: SomeClass2,
initialize: function() {
this.parent();
},
doSomething: function() {
this.grandParent();
}
});
来自 Class3
, child ,我需要调用doSomething()
方法来自 Class1
,祖 parent ,未执行 Class2#doSomething()
中的任何代码, parent 。
我需要的是 grandParent()
补充Mootools的方法parent()
, 但似乎不存在。
在 Mootools 或纯 JavaScript 中完成此任务的最佳方法是什么?谢谢。
更新:
我应该提到:我意识到糟糕的设计让我首先问了这个问题。 mixin 是理想的选择,但我继承了代码,目前没有时间进行重构。
最佳答案
如前所述,我打赌在这里使用 mixin 更有意义,但现在开始吧。
http://jsfiddle.net/rpflorence/24XJN/
var GrandParent = new Class({
initialize: function(){
console.log('init:GrandParent');
},
talk: function(){
console.log('talk:GrandParent');
}
});
var Parent = new Class({
Extends: GrandParent,
initialize: function(){
this.parent();
console.log('init:Parent');
},
talk: function(){
console.log('talk:Parent');
}
});
var Child = new Class({
Extends: Parent,
initialize: function(){
this.parent();
console.log('init:Child');
},
talk: function(){
GrandParent.prototype.talk.apply(this);
console.log('talk:Child');
}
});
关于javascript - 如何在 Mootools 中获得 this.grandparent() 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2975972/