I don't know if this is possible, but how I can call the method_one.function_one() inside the method_Two.function_Two ?
define(function (require) {
var language = require('../language.config');
return {
method_One: function(){
function function_One(data){
return true;
}
return {
function_One : function_One
}
},
method_Two: function(){
function function_Two(data){
//How to call the method_One.function_One() ?
getValueOfFunctionOne = this.method_One().function_One();
//Do something with result
return getValueOfFunctionOne;
}
return {
function_Two : function_Two
}
},
};
});
Is possible to do that? Or have any other way to do?
it should become something like this:
let obj = {
meth1: () => {
function func1() {
return true;
}
return {
func1: func1
}
},
meth2: () => {
// notice the use of "obj"
obj.meth2().func1()
}
}
// test it
obj.meth2(); // returns true
You can do this using local variable in the method_Two which will hold instance of the object which you are passing as a module to RequireJS:
define(function (require) {
var language = require('../language.config');
return {
method_One: function(){
function function_One(data){
return true;
}
return {
function_One : function_One
}
},
method_Two: function(){
var self = this;
function function_Two(data){
getValueOfFunctionOne = self.method_One().function_One();
//Do something with result
return getValueOfFunctionOne;
}
return {
function_Two : function_Two
}
}
};
});
Other way is to use bind to bind this to the instance of the module:
define(function (require) {
var language = require('../language.config');
return {
method_One: function(){
function function_One(data){
return true;
}
return {
function_One : function_One
}
},
method_Two: function(){
function function_Two(data){
getValueOfFunctionOne = this.method_One().function_One();
//Do something with result
return getValueOfFunctionOne;
}.bind(this);
return {
function_Two : function_Two
}
}
};
});
Third way is to assign the returned module to the variable and use it in the function:
define(function (require) {
var language = require('../language.config');
var module = {
method_One: function(){
function function_One(data){
return true;
}
return {
function_One : function_One
}
},
method_Two: function(){
function function_Two(data){
getValueOfFunctionOne = module.method_One().function_One();
//Do something with result
return getValueOfFunctionOne;
};
return {
function_Two : function_Two
}
}
};
return module;
});
Personally I think the third option is the best, it is very readable, open to extend.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.