[英]How do recursive function calls work in loops?
I have a function, in which there is a loop which calls up the function. 我有一个函数,其中有一个调用该函数的循环。
function displayItem(item, isChild)
{
if (isChild)
{
writeOutput('<li>' & item.name & '</li>');
}
else
{
writeOutput('<li>' & item.name);
}
try
{
if (item.hasChild)
{
writeOutput('<ul>');
numberOfItems = item.numChildren;
for (x=1;x LT numberOfItems;x++)
{
displayItem(item.child[x], true);
}
writeOutput('</ul>');
}
}
catch(Exception e){}
writeOutput('</li>');
}
After the function is executed, the loop continues off of the value of x from that execution, rather than picking up where it left off before. 函数执行完后,循环将从执行中继续取x的值,而不是从之前中断的位置开始取回。
For instance: x is 3. displayItem is passed "item.child[3]" and true. 例如:x为3。displayItem传递“ item.child [3]”和true。 It works through the function, enters the loop, performs the loop up to x = 4, then falls out of the loop.
它通过该函数起作用,进入循环,执行直到x = 4的循环,然后退出循环。 The function ends and (from what I understand) returns back to the point where x should be 3. Instead of picking up from 3, adding one (making it 4) and then performing the loop again, it picks up from 4 (the value from the "internal" recursively called loop).
函数结束,并且(从我的理解中)返回到x应该为3的点。与其从3拾取,不加一个(使其变为4),然后再次执行循环,它从4(值)开始从“内部”递归调用循环)。
I know that sounds incoherent, but I can't think of any other way to explain it. 我知道这听起来并不连贯,但是我想不出其他任何方式来解释它。
Is there something I'm doing wrong, or is this just a fact of life and something I have to work around? 我有做错什么吗,还是这只是生活的事实,我必须解决?
UPDATE: After looking at it more, it appears as though the earliest loop is exiting early.
更新:经过更多研究之后,似乎最早的循环正在提早退出。 Using a local variable for 'x' fixed the counting issue, but the loop just exits at 4 if looking at the previous example.
对“ x”使用局部变量可解决计数问题,但如果看前面的示例,循环仅从4退出。 It leaves before the condition is met.
它在满足条件之前就离开了。 Any ideas?
有任何想法吗?
You forgot to make x local to the function. 您忘记将x设置为该函数的局部值。
Don't use global variables. 不要使用全局变量。 Avoid them like the plague.
避免他们像瘟疫一样。
The problem is this line: 问题是这一行:
numberOfItems = item.numChildren;
When returning from the second call, this value is not changed back to the proper value. 从第二个调用返回时,此值不会变回正确的值。 So if
numberOfItems
is set to 4 when the function is called up by itself, after it has completed and returned to the original instance of the function, numberOfItems
is still 4. 因此,如果在函数本身完成调用并返回到该函数的原始实例后自行调用该函数时将
numberOfItems
设置为4,则numberOfItems
仍为4。
this code works: 此代码有效:
function displayItem(item, isChild)
{
var x = 1;
if (isChild)
{
writeOutput('<li>' & item.name & '</li>');
}
else
{
writeOutput('<li>' & item.name);
}
try
{
if (item.hasChild)
{
writeOutput('<ul>');
for (x=1;x LT item.numChildren;x++)
{
displayItem(item.child[x], true);
}
writeOutput('</ul>');
}
}
catch(Exception e){}
writeOutput('</li>');
}
Try this 尝试这个
function displayItem(item, isChild)
{
var x = 0;
var numberOfItems = 0;
if (isChild).......................
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.