JS函数尾调用优化
1 // 尾部调用 2 function f (x) { 3 console.log('这是函数f:'+ x) 4 return g(x) 5 } 6 function g (a) { 7 console.log('这是函数g:'+ a) 8 } 9 f(10) 10 // 尾递归:不会出现栈溢出现象,相对减少占用内存 11 function fac (n, total = 1) { 12 if(n === 1) return total 13 return fac(n - 1, n * total) 14 } 15 console.log(fac(5))//120