Max Stack exceeded 错误,我该如何解决?

Max Stack exceeded error, how would I fix this?

我正在创建一个函数,如果 outerfucntion 中的数字在 innerfunction 列表中

,则 returns 为真
<script>
function hasMatch(item) {
    hasMatch(2)  
    function inList() {
        var List = [1,2,3,4];
        for (i = 0; i<List.length; i++){
            if (list[i] == item) { 
                return true; 
            } else {
                return false;
            }
        }
    }
    inList();
}
hasMatch();
</script>

我得到一个 "Max Stack exceeded error",我该如何解决这个问题?

hasMatch(2)是没有任何终止条件的递归调用。

hasMatch() 被无限调用,这就是您看到堆栈超出错误的原因。

function hasMatch(item) { 
    function inList() {
        var List = [1,2,3,4];
        for (i = 0; i<List.length; i++){
            if (List[i] == item) { 
                return true; 
            } else {
                return false;
            }
        }
    }
    return inList();
}

hasMatch(2);