ES6函数的扩展

/* 箭头函数对比 */
    function a(a,b){
        return a+b;
    }
    let a1 = (a,b)=>a+b;//如果函数内部只有返回值可以这么写
    console.log(a(1,2),a1(1,2));

    function b(a,b){
        let c = 1;
        return a+b+c;
    }
    let b1 = (a,b) => {
        let c = 1;
        return a+b+c;
    }
    console.log(b(1,2),b1(1,2));

    let b2 = {
        name:"Agwenbi",
        age:25
    }
    //bind
    function b3(){
        console.log(this.name);
    }
    b3();//此时的函数内部的this指向window
    b3.bind(b2)();//此时的函数内部的this指向b2
    //apply
    function b4(a=0,b=0){
        console.log(a,b,this);
    }
    b4.apply(b2,[1,2]);//参数要用数组包裹,this指向b2
    b4.apply(b2);//没有参数可以不写,this指向b2
    //call
    function b5(a=0,b=0){
        console.log(a,b,this);
    }
    let b6 = [2,3];
    b5.call(b2,1,2);//与apply的不同就在于参数传递的形式
    b5.call(b2,...b6);
    /* 箭头函数对比 */

    /* 参数的默认值 */
    let c = (a = 0,b = 0) => a + b;
    console.log(c(),c(1,2));

    let c1 = (a = 0,b = a)=> a + b;
    console.log(c1(),c1(1),c1(1,2));
    /* 参数的默认值 */

    /* 扩展运算符 */
    let d = [1,2,3,4,5,6];
    let d1 = d;//此方法是直接将地址复制,后续操作d1会改变d
    let d2 = [...d];//反之
    console.log(d == d1,d == d2,d1 == d2);

    let d3 = (a,b,c,d,e,f)=> a + b + c + d + e + f;
    console.log(d3(...d));
    /* 扩展运算符 */

    /* rest运算符 */
    let e = (a,...args)=>{
        console.log(a,args);
    }
    e(1,2,3,4,5);
    /* rest运算符 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Agwenbi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值