ES6快速入门 - 默认参数

1.

/* eslint-disable */
{
    // ES5 \ ES3 默认参数的写法
    function f(x,y,z){
        if(y === undefined){
            y = 7;
        }
        if(z === undefined){
            z = 42;
        }
        return x + y + z;
    }
    document.writeln(f(1));                 //  50
    document.writeln(f(1,3));               //  46
}

{
    // ES6  默认参数
    function f(x,y = 7,z = 42){
        return x + y + z;
    }
    document.writeln(f(1));                     //  50
    document.writeln(f(1,3));                   //  46
}

2.



{
    // 检查参数是否为空
    function checkParameter(){
        throw new Error('can\'t be empty');
    }
    function f(x = checkParameter() , y = 7, z = 42){
        return x + y + z
    }
    console.log(f(1));
    try{
        f()
    }catch(e){
        console.log(e);
    }finally{

    }
}

这里写图片描述

3.

{
    // ES3,ES5  可变参数
    function f(x){
        console.log(x);
    }
    console.log(f(1,2,3));                  //  1
}

{
    // ES3,ES5  可变参数
    function f(){
        var a = Array.prototype.slice.call(arguments);
        var sum = 0;
        a.forEach(function(item){
            sum += item * 1;
        })
        return sum;
    }
    console.log(f(1,2,3,6));                //  12
}

{
    // ES6  可变参数
    function f(...a){                   //  ... 扩展运算符
        console.log('a = ',a);          //..a =  (4) [1, 2, 3, 6]
        let sum = 0;
        a.forEach((item) => {
            sum += item * 1;
        })
        return sum
    }
    console.log(f(1,2,3,6));                    //  12
}

4.

// ES5 合并数组
{
    var params = ['hello',true,7];
    var other = [1,2].concat(params);
    console.log(other);
}

这里写图片描述

// ES6 利用扩展运算符合并数组
{
    var params = ['hello',true,7];
    var other = [1,2,...params];
    console.log(other);
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/zjsfdx/article/details/80382096
今日推荐