美文网首页
ES6之函数优化

ES6之函数优化

作者: YAOPRINCESS | 来源:发表于2020-07-11 15:31 被阅读0次

    image.png

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <script>
            // 判断b是否为空,为空就给默认值1
            function add(a,b){
                b=b||1;
                return a+b;
            }
            console.log(add(10));//11
    
            //现在这样写
            function add1(a,b=1){
                return a+b;
            }
            console.log(add1(20));//21
    
    
            //不定参数
            function fun(...values){
                console.log(values.length)
            }
            fun(1,2,3);//3
            fun(1,2,3,4,5);//5
    
    
            //箭头函数
            //以前的写法
            var print=function(obj){
                console.log(obj);
            }
            print(1234);
            //现在
            var print1 = obj=>console.log(obj);
            print1(12345);
            var sum2=(a,b)=> a+b;
            console.log(sum2(11,22));
            var sum3=(a,b)=>{
                c=a+b;
                return a+c;
            }
            console.log(sum3(10,20));
    
            //解构
            const person={
                name:"jack",
                age:21
            }
            function hello(person){
                console.log("hello"+person.name);
            }
            var hello2=({name})=>console.log("hello"+name);
            hello2(person);
        
        </script>
        
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:ES6之函数优化

          本文链接:https://www.haomeiwen.com/subject/wadicktx.html