[英]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如何工作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.