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
2 Answers
Reset to default 9if 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 !
.
版权声明:本文标题:recursion - Recursive javascript function causing "Maximum call stack size exceeded" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034234a2417033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论