ES3,ES5,ES6的区别

默认参数:


{

es3.es5默认参数的写法:

function f(x,y,z){

if(y===undefined){

    y = 7;

}

if(z === undefined){

    z = 42;

}

return x + y +z

}

console..og(f(1,3));

}


{

es6默认参数:

function f(x,y = 7 , z= 42){

    return x + y+ z

}

console.log(f(1,3));

}
es6判断x 是否是空值


{

    function checkParameter(){

        throw new Error('can\'t be empty ')

}

    function f(x = checckParameter(),y = 7, z = 42){

    return x+y +z

}

console.log(f(1));

try{

    f()

}catch(e){

    console.log(e);

}finally{

}

}

可变参数:

{

    es3,es5可变参数:

    function f(){

    var a = Array.prototype.slice.call(arguments);

    var sum = 0;

    a.forEach(function (jitem){

        sum +=item*1;

})

    return sum

}

    console.log(f(1,2,3,4))

}

{

    es6可变参数:

    function f(...a){

    var sum = 0;

    a.forEach(item=>{

    sum+=item*1;

});

    return sum

}

}

合并数组:


{

    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/qq_36457584/article/details/80975831
今日推荐