【发布时间】:2021-09-28 13:06:28
【问题描述】:
我对函数递归感到困惑
function foo(i) {
console.log("checking: " + i);
if (i < 0)
return;
console.log('begin: ' + i);
foo(i - 1);
console.log('end: ' + i);
}
foo(1);
为什么是输出:
checking: 1
begin: 1
checking: 0
begin: 0
checking: -1
end: 0
end: 1
它不应该打印 end: 0, end: 1,因为我们在 if 条件下返回。发生了什么?
【问题讨论】:
标签: javascript function recursion