Deconstruction assignment of ES6 array (bottom)

Source: Internet
Author: User

Tag: The same color EFI tells the assignment code that the ONS codes are not

Deconstruction assignment of an object

The deconstruction assignment of an object is similar to the destructor assignment of an array, so let's look at a small code:

var {a,b,c} = {"A": 1, "B": 2, "C": 3};    Console.log (a);//Result: A has a value of 1        console.log (b);//Result: B has a value of 2    console.log (c);//Result: The value of C is 3

The code for this example is similar to the destructor assignment of an array, except that the array is replaced with an object. But there is a difference between the two, and we make a slight change to the above code:

   var {a,b,c} = {"A": 1, "C": 3, "B": 2};    Console.log (a);//Result: A has a value of 1    console.log (b);//Result: B has a value of 2    console.log (c);//Result: The value of C is 3

  

I swapped the position of object property B and property C on the right side, but this does not affect the result of the assignment, and the value of variable b and variable C does not change, still B is 2,c 3. This tells us that the object's deconstruction assignment is not affected by the order of the attributes (the array is affected), it is associated with the property name, and the variable name is consistent with the property name before the value is successfully assigned.

If a variable cannot find a property with the same name, the assignment is unsuccessful, as in the following example:

    var {a} = {"B": 2};    Console.log (a);//Result: The value of a is undefined

Variable A does not find the property A that matches the name on the right, so the variable A is not successful and the value of a is undefined.

But it's not completely out of the way, if you want to give a variable name that is different from the property name, you can write:

    var {b:a,} = {"B": 2};    Console.log (a);//Result: A has a value of 2

  

Deconstruction assignment of ES6 array (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.