JavaScript 使用 .call() 继承

JavaScript Inheritance Using .call()

我需要一种方法来调用许多已设置某些默认值的 DHTMLX attach*() 函数。这只是一个例子。如果我能想出这个例子,那么我就可以将它应用到所有其他人身上。

DHTMLX 有许多与此类似的函数:dhtmlXCellObject.prototype.attachToolbar()、attachTabbar()、attachRibbon() 等...但是对于我应用程序中的每个工具栏,我都希望自动应用某些设置比如 iconSize 和 iconPath。

dhtmlXCellObject.prototype.attachTheBetterToolbar = function (conf) {
    // https://docs.dhtmlx.com/api__dhtmlxlayout_attachtoolbar.html
    // dhtmlXToolbarObject.prototype.attachToolbar.call(this, conf); This throws: Cannot read property 'call' of undefined

    this.attachToolbar.call(this, conf);

    // I want these two settings below on every single toolbar in our app but
    // I only want to have to set them once in here.  Then throughout my
    // entire application, we will only use attachTheBetterToolbar...
    // layout.cells('a').attachTheBetterToolbar()
    // window.attachTheBetterToolbar()
    // accordian.attachTheBetterToolbar()
    // tabbar.tabs('a').attachTheBetterToolbar()
    // etc...

    this.setIconSize(18);
    this.setIconsPath(c3.iconPath);
};

上面的代码不起作用(错误为:this.setIconSize 不是一个函数)但我想您会明白我正在尝试什么。我正在阅读关于 JavaScript 扩展、应用、调用、继承等的各种文章...我觉得我很接近,但就是没有点击。

我认为“.call()”部分会导致继承发生,如下所述:(变体 1 - Mixin -> 继承示例)

这正是我需要的...

dhtmlXCellObject.prototype.attachTheBetterToolbar = function (conf) {
    var tb = this.attachToolbar(conf);

    // this.setIconset("awesome");
    tb.setIconSize(18);
    tb.setIconsPath(c3.iconPath);

    return tb;
};