Deconstruction ES6 variable assignment (b) the object of deconstruction assignment

Earlier we know, the structure of the array assignment requires assignment in order,

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

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

 

Deconstruction object is assigned by the object attribute assignment, the order is not required, but was unable to match the undefined

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

In fact, the complete deconstruction of writing assignment object is:

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

E.g. above example, the left hand side is the same as the variable name and the name of the attribute, can be written directly {A the let, B, C} = {B: . 1, A: 2 };

 

Similarly, if the property name and variable names are different, can not be abbreviated

 

{a the let: b = {a}: . 1 }; 
the console.log (B); // . 1
 // NOTE: b is a variable name, a property name can not be a direct output

 

 

Deconstruction assignment objects can also set the default value of variables, the default value for the entry into force: the value of object properties strict ( "===") equal to undefined

var {x = 3} = {};//x==>3

var {x = 3} = {x: undefined};//x==>3

var {x = 3} = {x: null};//x==>null

var {x = 3} = {x:0};//x==>0
var {x: y = 3} = {};//y==>3 

 

Array itself is a special object that can be deconstructed assignment object

var arr=[1,2,3];
 let {0:first,[arr.length-1]:last}=arr;
console.log(first);//1
console.log(last);//3

 

Object deconstruction application assignment:

1 can be easily acquired property or method existing object

// Console object log property has a print function,
 // Console object is assigned to the object created in the log, it is possible to directly log () function enables the printing 
const {} = log Console; 
log ( ' Hello ' ) // Hello equivalents in the console.log ();

 

 

 

 

Guess you like

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