zoukankan      html  css  js  c++  java
  • es6-函数扩展

    参数默认值

    function test(x,y='world'){ console.log('默认值',x,y); }
    
    test('hello');
    
    test('hello','eeee');

    这里的world就是默认值,也可以直接传参

    但是默认值后面不能再有没有默认值的变量

    function test(x,y='world',c)

    这种是不行的

    再讲一下作用域

    { let x='a'; function test2(x,y=x) { console.log('作用域:',x,y); } }
    
    test2();
    
    test2('b');

    这里在大括号里有一个x=‘a’;

    在作用域外传参也会传x

    但是大括号里let x='a';是无效的

    test2(1,2)输出的是1,2

    但是如果传的参是这样的:function test2(c,y=x){ console.log('作用域:',c,y); }

    test2(1)输出的结果会是1,a

    这个时候let x='a'就有效了

    rest参数

    { function test3(...arg){ for(let v of arg) { console.log('rest',v); } } }
    
    test3(1,2,3,4,'a');

    rest参数后不能有其他参数了,否则会报错

    扩展运算符:...

    console.log(...[1,2,3])

    输出1,2,3

    相当于将数组拆成了一个离散的值

    箭头函数

    { let arrow=v=>v*2;
    
    console.log('arrow',arrow(3));
    
    let arrow2=()=>5;
    
    console.log('arrow2',arrow2());
    }

    这里arrow相当于一个函数

    fuction arrow(v){

    return v*2;

    }

     

    尾调用

    { function tail(x)
    
    { console.log('tail',x); }
    
    function fx(x) { return tail(x); }
    
    fx(123); }

    这种形式是尾调用,可以提升性能

  • 相关阅读:
    python_58_装饰器1
    python_57_高阶函数
    python_56_递归
    python_55_局部和全局变量
    python_54_函数调用函数
    python_53_函数补充
    python_52_函数返回值2
    mysql 使用 GROUP BY 时报错 ERROR 1055 (42000)
    sql之left join、right join、inner join的区别
    C++ 函数的重载和参数默认值
  • 原文地址:https://www.cnblogs.com/ellen-mylife/p/11083002.html
Copyright © 2011-2022 走看看