簡體   English   中英

如何在dojo中調用另一個方法的父方法

[英]how to call the parent method of another method in dojo

我怎么能在dojo中調用另一個方法的父方法。 考慮以下示例:

var parent = declare(null,{

m1: function(arg){
console.log("parent.m1");
},
m2: function(arg){
console.log("parent.m2");
}

});`enter code here`

var child = declare(parent,{

m1: function(arg){
console.log("child.m1");
// how can i call parent.**m2** here directly without calling child.m2
},
m2: function(arg){
console.log("child.m2");
}

});

我怎么能直接從child.m1調用parent.m2而根本不調用child.m2

現在假設我定義了以下兩個模塊:

parentModule.js

    var parent = declare(null,{

    m1: function(arg){
    console.log("parent.m1");
    },
    m2: function(arg){
    console.log("parent.m2");
    }

    });
    return declare("ParentModule",[parent,child]);
//******************************************//
childModule.js

    return declare("child",null,{

    m1: function(arg){
    console.log("child.m1");
    // how can i call parent.**m2** here directly without calling child.m2
    //if we call ParentModule.prototype.m2.call(this,arguments); this will call child.m2
    //as child module override the parent now
    //also calling this.getInherited("m2",arguments); will call child.m2 !!!
    //how to fix that?
    },
    m2: function(arg){
    console.log("child.m2");
    }

    });

使用dojo的聲明時,可以在this.inherited(arguments)中使用this.inherited(arguments)來調用父函數,請參見:

http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#dojo-base-declare-safemixin

m1: function (arg) {
    console.log("child.m1");
    this.inherited(arguments);
}

您可以使用javascript的原型功能來完成您要的內容。

m1: function(arg){
    console.log("child.m1");
    parent.prototype.m2.apply(this, arguments);
},

有關原型的更多信息,請參見此處。JavaScript .prototype如何工作?

這是此工作示例http://jsfiddle.net/cswing/f9xLf/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM