javascript - 从命名空间中使用递归函数时为 "Too much recursion"

标签 javascript jquery recursion javascript-namespaces

我在 javascript 中使用了一个递归方法,它工作得很好,直到我把它放在一个命名空间中。该函数返回一个元素,该元素具有给定的 quoteproduct id 作为数组中的 id 属性。它是一个嵌套数组,这就是函数递归的原因。这是函数声明:

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;
}

这就是我声明命名空间的方式:

var QuoteProductService = QuoteProductService || {};

这是我在函数中使用的命名空间中的数组:

QuoteProductService.QuoteProductConfigurations = [];

该数组在页面加载时填充。

现在,每当我调用该函数时,都会收到“太多递归”错误。我究竟做错了什么 ?同样,这个函数在我将函数和数组放入命名空间之前就起作用了。

最佳答案

我刚刚用更简单的变量名重写了您的代码:

var a = {
    b: = [{id: 1}, {id: 2}, {id: 3}]
};
a.get = function( searchId ) {
    var match = null;

    $.each(a.b, function(key, value) {
        if ( value.id === searchId ) {
            // Yes we found the match, break and everything

            match = value;
            return false;
        }
        else {
            match = a.get();

            if ( match ) {
                return false;
            }
        }
    });
    return match;
};

a.get(1) // will return {id: 1}
a.get(2) // will throw recursive error

为什么?

由于您的结构,您总是将 $.each 指向 a.b

因此它是这样的:

Loop over a.b: a.b[0].id === searchId ?
Ok everything is good return first value

if not a.b[0].id === searchId
Loop over a.b
a.b[0].id === searchId ?
Ok everything is good return first value
if not a.b[0].id === searchId
Loop over a.b
.....

希望你明白:

要解决这个问题,您需要指定我们必须循环的数组:

QuoteProductService.getQuoteProduct = function (quoteproductid, loopArray) {
    var founditem = null;

    // if (loopArray) {loopArray = loopArray} else { loopArray=Quote...QuteConfig.. }
    loopArray = loopArray || QuoteProductService.QuoteProductConfigurations;

    $.each(loopArray, function (index, item) {
        if(item.id == quoteproductid) {
            founditem = item;
            return false; // break the $.each if an item is found
        } else {
            founditem = QuoteProductService.getQuoteProduct(quoteproductid, item.children);
            if(founditem != null) return false; // break the $.each if an item is found
        }
    });
    return founditem;
}

关于javascript - 从命名空间中使用递归函数时为 "Too much recursion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419098/

相关文章:

javascript - d3 - 我可以以某种方式组成两个投影,或者投影一个投影吗?

javascript - 基于 jQuery window.width() 的动态边距

jquery:根据类名变量隐藏/显示

javascript - 即使我使用了 .each,为什么只有一个元素会触发该操作?

python - 将递归问题代码从 Python 转换为 Common Lisp

javascript - 使用扩展参数连接字符串的最有效方法

javascript - 类似于 weebly 页面编辑器的丰富内容编辑器

javascript - PDF 不显示在 IE9、10 和 11 的 fancybox 2 iframe 中

java - 计算java中递归调用的次数

python - 跳棋算法 : how to reduce nested for loops