javascript - 如何在 Mootools 中获得 this.grandparent() 功能?

标签 javascript mootools

我在 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/

相关文章:

javascript - 将 jQuery 事件作为变量传递

javascript - 视频有时可以很好地加载,有时却需要很长时间才能加载,或者根本不加载?

javascript - 如何检查 Twitter Bootstrap 是否存在?

javascript - MooTools 插件

jquery - 如何使用 jQuery 或 mootools 触发 a 标签的 native (href)点击?

javascript - 提示下载位置而不是直接下载

javascript - 移至选项卡但不重新加载

c# - 如何在单击时获取图像控件上的坐标

javascript - mootools 类型函数

Javascript:更改滚动条中可滚动元素句柄的大小