webveuje/js/kejian/digui.md
2021-03-23 10:58:10 +08:00

40 lines
902 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 递归
### 发生前提:
递归是在一个函数通过名字调用自身的情况下发生的
如:
```
function a(n){
if(n<=1){
return 1
}else{
return num*a(n-1)
}
}
```
这是一个经典的递归阶乘函数,但是接下来的操作却可能会导致他出错
```
function a(n) {
if (n <= 1) {
return 1
} else {
return n * a(n - 1)
}
}
console.log(a(5)) //120
var cheng=a
a=null
console.log(cheng(5))// : a is not a function
```
以上代码把a保存在一个变量cheng然后将a赋值为null
在cheng执行的过程中 必然会执行到a (在cheng 中else代码块的函数名字改不了)
但是此时a已经不是函数了所以会报错 a is not a function
上面问题的解决措施: