ES6 - Deconstruct assignment number, bool, string, array, function, object

In ES5, the assignment of various variables and objects can only be one-to-one. es6 has been reformed. The new standard allows us to assign in groups. Variables correspond to the position of values and assign one by one. This is deconstruction assignment.

string String string Deconstruction Assignment:

let [w,b,i]='wbi';
console.log(w,b,i);//w b i

let [w,b]='wbiokr';
console.log(w,b);//w,b

let [w,b,i,o]='wb';
console.log(w,b,i,o);//w,b,undefined,undefined

let [(w)]='wbiokr';//Parentheses, report the error at this time! ___________

[(w)]='wbiokr';
console.log(w);//w In the current case, parentheses do not report errors
When string deconstructs an assignment, the key-value pair is always one-to-one correspondence. The extra value is not assigned to the last key, and the extra key is undefined.

Array array deconstruction assignment

let [a,b,c]=[1,2,3];
console.log(a,b,c);//1,2,3

let [a,b]=[1,2,3,4];
console.log(a,b);//1,2

let [,a,b]=[1,2,3,4];
console.log(a,b);//2,3

let [a,b,c]=[1];
console.log(a,b,c);//1,undefined,undefined

let [a,b,c]=[1,2,[3]];
console.log(a,b,c);//1,2,[3]

let [a,[b]]=[1,2];//Report errors!!!!!!!!!

let [a,[b]]=[1,[2]];
console.log(a,b);//1,2

let [a,b]={a:1,b:2}//Report errors!!!!!!!!!!

let [(a)]=[1];//Wrong report, bracket question!!!

[(a)]=[1];
console.log(a);//1. In the current case, parentheses do not report errors
Location corresponds, type corresponds!

object deconstruction assignment:

let {a}={a:1};
console.log(a);//1

let {b}={a:1};
console.log(b);//undefined

let {a:c}={a:1};
console.log(c);//1

let a;
let {a}={a:1};//Error reporting, when associated with let's characteristics, can not be redefined

let a;
{a}={a:1};
console.log(a);//No error was reported. Result: 1

let c;
let {a:c}={a:1};//Reporting errors, the same

let c;
{a:c}={a:1};
console.log(c);//1

let {a,b:{c}}={a:1,b:{c:3}};
console.log(a,b,c);//1,undefined,3

let {(aaaaq)}={aaaaq:1};//Error reporting, parentheses

let {a:(a)}={a:1};//Error reporting, parentheses

{a:(a)}={a:1};
console.log(a);//1 In the current case, parentheses do not report errors
Like array deconstruction assignment, as long as the location is corresponding, whether nested or not, nested several layers, can be successfully assigned, but when deconstruction assignment, we must pay attention to the format. A bracket problem can kill people if they are not careful in the development process.

function deconstruction assignment:

(function([a,b,c]){console.log(a,b,c)})([1,2,3]);//1,2,3

[[1,2],[3,4]].reduce(function([a,b]){console.log(a*b)});//2 , 12


Deconstruction assignment can also assign parameters of scoped functions, and the same function parameters can assign default values.

Default values in Deconstruction assignments:

let [a=1]=[];
console.log(a);//1

let {a,b=true}={a:2};
console.log(a,b);//2,true

let {a=true}={a:NaN};
console.log(a);//NaN

let {a=true}={a:null};
console.log(a);//null;

let {a=true}={a:undefined};
console.log(a);//true

function fun(x=1,y=2){
    console.log(x,y)
};
fun('s');//'s 2
fun(2,5);//2,5
fun();//1,2 defaults to 1,2

function fun2({x,y}={x:1,y:2}){
    console.log(x,y);
}//The default parameter value of fun2 function is to earn a parameter object
fun2();//1,2 also outputs default values
fun2({x:2});//2 undefined
Default values are absolutely good things, for example, when we call encapsulated ajax functions, page initialization, will automatically call once, at this time you can not pass the value.

———-

Deconstruction assignment exchange x and y values:

Once I was asked how to exchange the values of variables x and y without defining a third variable. Finally, it did, but it really killed more than N of my brain cells.
//Define X=2, Y=5 and exchange X, Y without the help of the third value. The result should be x=5,y=2.

var x=2,y=5;
x=y-x;//At this point x=3,y=5
y=y-x;//At this point x=3,y=2
x=y+x;//At this point x=5,y=2

///////If hard es6 What about structural assignment?.......
let x=2,y=5;
[x,y]=[y,x];
console.log(x,y)//5,2 Perfect!!!!!!!

Posted by themightydude on Mon, 11 Feb 2019 06:21:18 -0800