ES6 study notes - 2. destructuring assignment

2.1. Array deconstruction assignment

{
    // 数组
    let a,b,c;
    [a,b,c]=[1,2,3];
    console.info(a,b,c)
}

Output:

{
    let [a,...b]=[1,2,3,4,5,6];
    console.info(a,b);
}

Output:

{
    let a,b,c;
    [a,b,c]=[1,2];
    console.info(a,b,c);
    [a,b,c=3]=[4,5];
    console.info(a,b,c);
}

 

Output:

2.2 Object deconstruction assignment

{
    let a,b;
    ({a,b}={a:4,b:5})
    console.log(a,b);
}

 

Output:

 

{
    let o={p:42,q:true};
    let {p,q}=o;
    console.log(p,q);
}

 

Output:

 

 

2.3 deconstruction application - variable swap

{
    let a=1;
    let b=2;
    console.info(a,b);
    [a,b]=[b,a];
    console.info(a,b);
}

Output:

2.4 Application of deconstruction - a plurality of function return values

{
    function test()
    {
        return [1,2,3];
    }
    let [a,,c]=test();
    console.info(a,c)
    //输出1 3
}

Output:

2.5 deconstruction application - JSON assignment

{ 
    // end is returned to the data 
    the let returnJson = { 
        title: 'ABC' , 
        Test: [{ 
            title: 'Test' ,  desc: 'Description'  }]  }; 
     the let {title: esTitle , Test: [{title: cnTitle } ]} = returnJson; console.info (esTitle, cnTitle);}

 

Output:

Guess you like

Origin www.cnblogs.com/feihusurfer/p/12152399.html