Reputation: 1543
I need validateText
function to call modalFunc
. How can I do that?
function geoAlert(lodash) {
var me = this;
return {
modalFunc: function(text) {
alert(text);
},
validateText: function() {
modalFunc("hello")
}
}
}
When I run:
geoAlert.validateText();
I will get this error:
ReferenceError: modalFunc is not defined
Using me.modalFunc("hello")
does not work too. Please help and thanks in advance.
Upvotes: 1
Views: 52
Reputation: 2308
It looks like you're trying to use a revealing module pattern; returning an object that effectively provides methods. For this to work you need to execute your main function as follows (note the '()' at the end meaning it will be immediately invoked):
var geoAlert = function(lodash) {
return {
modalFunc: function(text) {
alert(text);
},
validateText: function() {
this.modalFunc("hello")
}
};
}();
geoAlert.validateText('Some text');
The other change I've made is that modalFunc will need to be prefixed by this as it exists as part of the same returned object as validateText.
Upvotes: 3
Reputation: 816262
You can create a named function:
function geoAlert(lodash) {
function modalFunc(text) {
alert(text);
}
return {
modalFunc: modalFunc,
validateText: function() {
modalFunc("hello")
}
}
}
Then doesn't matter how validateText
is called. But you should still learn about this
, since you attempted something with var me = this;
which probably doesn't do what you want anyway.
Upvotes: 2