how could i call the parent method of another method in 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
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");
}
});
how could i call the parent method of another method in 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
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");
}
});
Share
Improve this question
edited Mar 6, 2013 at 20:28
user1040987
asked Mar 6, 2013 at 14:18
user1040987user1040987
832 silver badges9 bronze badges
1
- 1 I am confused by your hierarchy. Why is Parent extending the Child? Shouldn't it be the other way around? I posted a jsfiddle that shows how I think you should define your modules. – Craig Swing Commented Mar 6, 2013 at 21:23
2 Answers
Reset to default 7When using dojo's declare you can use this.inherited(arguments)
in the child function to call the parent function, see:
http://dojotoolkit/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 acplish what you are asking.
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?
Here is an example of this working http://jsfiddle/cswing/f9xLf/