I am having a devil of a time trying to figure this out. I have a function that returns the value of another function. Within the second function I have a tertiary conditional and it will NOT get that return.. I can’t figure it out. I am sure it is a scope issue, but i can’t solve it. This is all within a jquery plugin.
somefunctionA: function(){
var self = this;
var data = somecollection_of_data;
var storeReturn = $.map( data, function( obj, i) {
return (function(i){
return self._returnFilteredData(id);
})(obj.i);
});
},
__returnFilteredData: function(i){
var self = this;
if(some_conditionals){
return (
self.url ?
(function(){
if(some_more_conditionals){
self._getDBdata(); <<-- NEVER FIRES OFF
}
}) : self._getDBdata() <<--- THIS ONE FIRES OFF
)
}
}
},
so what ends up happening is I get a “null” in the storeReturn array because in some conditionals that first tertiary fails. I cannot get into it… to test.
the “_getDBdata()” function just returns a hash based on some other data.
Any ideas on what is happening? I tried everything and I can’t solve this scope/closure issue.
First: this is horribly convoluted and you should stop doing it this way. Break it out into separate pieces.
Second: you’re never calling the anonymous function in that situation, you’re simply returning it. Adding another set of
()will fix that, so that you call it and return its value: