es6之函数扩展与对象扩展

一、函数扩展

1、参数默认值

参数有默认值,后面不可以再加没有默认值的变量。如以下test函数中,不可以加写成

function test(x,y="word",z){

}

function test(x,y="word"){
        console.log("默认值",x,y);
    }
    test("lala"); //lala word
    test("lala","你好啊"); //lala 你好啊

2、作用域的问题

//作用域的问题
    let x="test";
    function test2(x,y=x){
        console.log("作用域",x,y);
    }
    test2("kill"); //kill kill
test2() //undefined undefined

function test3(c,y=x){
console.log("作用域",c,y);
}
test3("kill"); //kill test

  

3、参数问题 

...arg后不可再加参数

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

console.log("a",...[1,2,3]); //a 1 2 3

4、伪调用  主要用于提升性能

 //伪调用
    function trail(x){
        console.log("tail",x);
    }

    function fx(x){
        return trail(x)
    }
    fx(123);

  

二、对象扩展  

  1、简洁表示法

 //简洁表示法
    let o=1;let k=2;
    let es5={
        o:o,
        K:k
    };
    let es6={
        o,k
    }
    console.log(es5,es6);

    let es5_method={
        hello:function(){
            console.log("11hello");
        }
    }
    let es6_methods={
        hello(){
            console.log("22hello")
        }
    }

    console.log(es5_method.hello(),es6_methods.hello());

2、属性表达式

//属性表达式
    let a="b";
    let es5_obj={
        a:"c"
    }
    let es6_obg={
        [a]:"c"
    }
    console.log(es5_obj,es6_obg);

3、新增API

  //新增api
    console.log("字符串",Object.is("abc","abc"),"abc"==="abc"); //true true
    console.log("数组",Object.is([],[]),[]===[]);  //false false

    console.log("拷贝",Object.assign({a:"a"},{b:"b"})); //a:"a",b:"b"

  

  

  

原文地址:https://www.cnblogs.com/karila/p/7865789.html