Deconstruction Deconstruction assignment assignment ES6 variables (a) of the array

let[a,...arr]=[1,2,3,4];//a==>1   arr==>[2,3,4]

let [x, y, ...z] = ['a'];//a==>'a'  y==>undefined  z==> []

let [a, [b], d] = [1, [2, 3], 4];//a==>1  b==>2  c==>4
let[a,b]=[1,2,3];//a==>1  b==>2

 Left and right sides of the array correspond one variable:

let[a,b,c]=[1,2,3];//a==>1  b==>2  c==>3
let[a,[[b],c]]=[1,[[2],3]];//a==>1  b==>2  c==>3

 The number is lower than the variable on the left to the right

let[a,b,]=[1,2,3];//a==>1  b==>2
let[a,,c]=[1,2,3];//a==>1 c==>3
let [a, [b], d] = [1, [2, 3], 4];//a==>1 b==>2 c==>4

 The number left more than the right amount, not match numeric variables are undefined

let[a,b,c]=[1,,3];//a==>1  b==>undefined   c==>3

Special: arr array

let[a,...arr]=[1,2,3,4];//a==>1   arr==>[2,3,4]
let[a,...arr]=[1,];//a=>1 arr=>[]

 

 

At the same time deconstructing the assignment can use the default values

Note: Only when the array members strictly equal undefined, the default value is valid
let[a,b='b']=['a'];//a==>'a'  b==>'b'
let[a,b='b']=['a',undefined];//a==>'a'  b==>'b'

let[a=1]=[null];//a==>null let[a=1]=[];//a==>1; let[a=1]=[undefined];//a==>1

 

If the default value is an expression, then this expression is lazy evaluation, that is, only when in use, will be evaluated
 
F function () { 
  the console.log ( 'AAA'); 
} 

the let [X = F ()] = [. 1]; // X ==>. 1; 

the let [Y = F ()] = []; 
Y; At this time, it is equivalent to y // F (); i.e. aaa result

 

The default value can refer to deconstruct other variable assignment, but the variable must already be declared.
let[a=1,b=a]=[];//a==>1  b==>2
let[a=b,b=1]=[];//报错

 

Guess you like

Origin www.cnblogs.com/kongbaifeiye/p/11967332.html