admin管理员组

文章数量:1318014

During the course of designing a small API at work and trying to make my functions as flexible as possible I decided to start adding checks for whether arguments are passed, and then based on that, do different things. So when the function is called with a number, the function uses the number as an index in an array. If no number is passed, I wanted the function to call itself as many times as the length of the array. However I get the call stack error. I have boiled the problem down to the recursion aspect of the function, which I'm listing below. The thing that is strangest to me is this...

THIS CAUSES ERROR

function testing(a){
    if(!a){
        for(var i = 0; i < 3; i += 1){
            testing(i);
        }
    }else{
        alert(a);
    }
}

testing();

THIS DOES NOT CAUSE ERROR

function testing(a){
    if(!a){
        for(var i = 0; i < 3; i += 1){
            testing(5);//Just adding hard coded number instead
        }
    }else{
        alert(a);
    }
}

testing();

I'm trying to understand why passing the var in the call throws an error. It seems that if the js engine can hold the initial function call in memory in order to make the for loop work properly why couldn't it hold a reference to i while calling itself? I feel like I am missing something fundamental here. I've tried lots of rewrites involving things like:

testing(function(i){return i;}(i));

All to no avail. This is driving me crazy and I would like to understand what is going on here.

During the course of designing a small API at work and trying to make my functions as flexible as possible I decided to start adding checks for whether arguments are passed, and then based on that, do different things. So when the function is called with a number, the function uses the number as an index in an array. If no number is passed, I wanted the function to call itself as many times as the length of the array. However I get the call stack error. I have boiled the problem down to the recursion aspect of the function, which I'm listing below. The thing that is strangest to me is this...

THIS CAUSES ERROR

function testing(a){
    if(!a){
        for(var i = 0; i < 3; i += 1){
            testing(i);
        }
    }else{
        alert(a);
    }
}

testing();

THIS DOES NOT CAUSE ERROR

function testing(a){
    if(!a){
        for(var i = 0; i < 3; i += 1){
            testing(5);//Just adding hard coded number instead
        }
    }else{
        alert(a);
    }
}

testing();

I'm trying to understand why passing the var in the call throws an error. It seems that if the js engine can hold the initial function call in memory in order to make the for loop work properly why couldn't it hold a reference to i while calling itself? I feel like I am missing something fundamental here. I've tried lots of rewrites involving things like:

testing(function(i){return i;}(i));

All to no avail. This is driving me crazy and I would like to understand what is going on here.

Share Improve this question edited Dec 31, 2013 at 16:14 carter asked Dec 31, 2013 at 16:05 cartercarter 5,4525 gold badges33 silver badges40 bronze badges 1
  • 4 It seems like you're passing in 0 every time on the first iteration of the loop, so that's always going to pass if(!a) and start another recursive call, which in turn does the same thing. – splrs Commented Dec 31, 2013 at 16:09
Add a ment  | 

2 Answers 2

Reset to default 9

if a===0 it is false, that means you have an infinite loop

for more details, read this and this (thanks @yochannah )

This works just fine.

function testing(a){
  if(!a || a == 0){
    for(var i = 1; i <= 3; i++){
      testing(i);
    }
  } else {
    alert(a);
  }
};

testing();

Obviously there's no need to both start the loop counter off of 1 AND checking whether a == 0, but both methods would work.

EDIT: Another check you can have is (a == undefined).

The thing is that a variable that returns 0 is false when you check it with !.

本文标签: recursionRecursive javascript function causing quotMaximum call stack size exceededquotStack Overflow