ES6语法学习笔记(7)-函数扩展

默认值使用

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

作用域问题

{
    let x='test';
    function test2(x,y=x){
        console.log('作用域',x,y);
    }
    test2('kill')
    //test2()//不赋值时为undefined undefined
}

rest 参数 相当于es5中的arguments,rest参数将离散的值转换成数组

arguments 方法

{
    function func(a, b){
    var args = Array.prototype.slice.call(arguments);
    console.log(args)
}
    func(1,2)
}

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

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

}

扩展运算符将数组转化成离散的值

{
    console.log(...[1,2,4]);//1 2 4
    console.log('a',...[1,2,4]);//a 1 2 4
}

箭头函数

{
    let arrow = v => v*2;//带参数
    let arrow2 = () => 5;//不带参数
    console.log(arrow(3));//6
    console.log(arrow2());//5
}

伪调用,递归涉及函数嵌套,提升性能优化

{
    function tail(x){
        console.log('tail',x);
    }
    function fx(x){
        return tail(x)
    }
    fx(123);//tail 123
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值