<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>函数</title></head><body></body><script>
function fun1(x,y){return x+y
}
let a =fun1(1,2);
console.log(a);//3// 函数参数默认值
function fun2(x=1,y=0){return x+y
}
let b =fun2();
console.log(b);//1// 函数多参数
function fun3(...n){
console.log(n.length);}fun3(1,2,3);//3// 箭头函数
let fun4 =(v,y)=>{
console.log(v,y);}fun4('hello','world');// hello
let fun5=(x)=> x+1// 或这样写。let fun5 = x =>x+1
console.log(fun5(1));//2// 函数尾调用// 尾调用,共用一个内存空间。一定要注意一点,尾调用的函数一定是最后一步(区分最后一行),切不参与运算。
let fun6 =(x)=>x+5;
let fun7 =()=>{returnfun6(2)}
console.log(fun7());//7// 不属于尾调用
let fun8 =(x)=>x+2;
let fun9 =()=>{
let a =fun8(2);return a
}
console.log(fun9());// 4. 虽然也可以输出4,但是不属于尾调用。// 不属于尾调用
let fun10 =()=>{returnfun8(9)+1}
console.log(fun10());//12 . 参与运算了。这是不可以的,参与尾调用的必须是独立的。不能有什么牵连。// 尾调用优化// 递归
let factorial =(n)=>{if(n <=1){return1}else{return n*factorial(n-1)}}
console.log(factorial(3));//1*2*3