简体   繁体   English

如何在dojo中调用另一个方法的父方法

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

how could i call the parent method of another method in dojo. 我怎么能在dojo中调用另一个方法的父方法。 consider the following example: 考虑以下示例:

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");
}

});

how can i call parent.m2 directly from child.m1 without invoking child.m2 at all 我怎么能直接从child.m1调用parent.m2而根本不调用child.m2

now suppose i define two modules as the following: 现在假设我定义了以下两个模块:

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");
    }

    });

When using dojo's declare you can use this.inherited(arguments) in the child function to call the parent function, see: 使用dojo的声明时,可以在this.inherited(arguments)中使用this.inherited(arguments)来调用父函数,请参见:

http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#dojo-base-declare-safemixin 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);
}

You can use the javascript's prototype functionality to accomplish what you are asking. 您可以使用javascript的原型功能来完成您要的内容。

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

More about prototype can be found here How does JavaScript .prototype work? 有关原型的更多信息,请参见此处。JavaScript .prototype如何工作?

Here is an example of this working http://jsfiddle.net/cswing/f9xLf/ 这是此工作示例http://jsfiddle.net/cswing/f9xLf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM