Deconstruction variable assignment 3-

Previously, assigning a variable, we can only specify the value directly. For example the following code:

let a=0;
let b=1;
let c=2;

And now we can use an array of deconstruction way to carry out the assignment.

letl  [a,b,c]=[1,2,3];

It represents the above code can be extracted from the array values, according to the relationship between the position of the object variable.

** array mode and unified assignment mode: **

It can be simply understood as the form of the equal sign on the left and right of the equal sign to be unified, if not uniform deconstruction will fail.

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

If not the same on both sides of the equal sign the form, it is possible to obtain direct or undefined error.

Deconstruction Default:

Deconstruction assignment is to allow you to use the default values, look at a simple example is the default.

let [foo = true] =[];
console.log(foo); //控制台打印出true

Examples of the array on top of only one value, you may be somewhat confused, we come to an array of a plurality of values, and give him some defaults.

let [a,b="JSPang"]=['小麦']
console.log(a+b); //控制台显示“技术胖JSPang”

Now that we understand the default should be noted that the difference between undefined and null.

let [a,b="JSPang"]=['小麦胖',undefined];
console.log(a+b); //控制台显示“小麦”

undefined what amounts have not, b is the default value.

let [a,b="JSPang"]=['小麦',null];
console.log(a+b); //控制台显示“小麦”

the equivalent of null value, but the value is null. So b is not the default value, but deconstruction became null.

Deconstruction object assignment

Deconstruction not only can be used in an array, it can also be used for objects.

let {foo,bar} = {foo:'JSPang',bar:'技术胖'};
console.log(foo+bar); //控制台打印出了“小麦技术胖”

Note: Deconstruction and an array of objects there is an important difference. Elements of the array are arranged in order, the value of the variable is determined by its location; no order and properties of the object, attribute variables must be the same name, in order to get the correct value.

Use parentheses

If prior to deconstruct it defines a variable, then this time you deconstruct problems. Here is the error code, the compiler will complain.

let foo;
{foo} ={foo:'JSPang'};
console.log(foo);

To solve the error, the normal procedure, this time as long as we deconstruct statement outside plus a parenthesis on it.

let foo;
({foo} ={foo:'JSPang'});
console.log(foo); //控制台输出jspang

String deconstruction

String may be deconstructed because, at this time is converted into a string array-like object.

const [a,b,c,d,e,f]="JSPang";
console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);
console.log(f);

Summary: This lesson is very simple, but there are some details that need attention, I hope you can put hands deconstruction exercises again, deconstruction Json data format in real projects is still very prevalent, with the ES6 have to help improve the lot of work effectiveness.

 

Guess you like

Origin www.cnblogs.com/mahmud/p/11563449.html