I’m using a recursive method in javascript which worked perfectly fine, until I put it in a namespace. The function returns an element which has the given quoteproduct id as it’s id attribute from an array. It’s a nested array, that’s why the function is recursive. This is the function declaration :
QuoteProductService.getQuoteProduct = function (quoteproductid) {
var founditem = null;
$.each(QuoteProductService.QuoteProductConfigurations, function (index, item) {
if(item.id == quoteproductid) {
founditem = item;
return false; // break the $.each if an item is found
} else {
founditem = QuoteProductService.getQuoteProduct(item.children, quoteproductid);
if(founditem != null) return false; // break the $.each if an item is found
}
});
return founditem;
}
This is how I’m declaring the namespace :
var QuoteProductService = QuoteProductService || {};
and this is the array in the namespace that I’m using in the function :
QuoteProductService.QuoteProductConfigurations = [];
This array is populated when the page loads.
Now, whenever I call the function, I get a “too much recursion” error. What am I doing wrong ? Again, this function worked before I put the function and array in a namespace.
I have just rewritten your code with simpler variable names:
Why?
Because of your structure, As you always points the
$.eachtoa.b.And therefore it goes something like this:
Hope you understand:
To fix this you will need to specify what array that it is we have to loop over: