前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >重学ES系列之函数优化

重学ES系列之函数优化

作者头像
马克社区
发布2022-06-16 18:48:53
2010
发布2022-06-16 18:48:53
举报
文章被收录于专栏:高端IT高端IT
代码语言:javascript
复制
<!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 =()=>{
        return fun6(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 = () =>{
        return fun8(9)+1
    }
    console.log(fun10());//12 . 参与运算了。这是不可以的,参与尾调用的必须是独立的。不能有什么牵连。
    // 尾调用优化
    // 递归
    let factorial = (n)=>{
        if(n <=1 ){
            return 1
        } else{
            return n*factorial(n-1)
        }
    }
    console.log(factorial(3)); //1*2*3

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119862109

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档