ES6 Sharing--the deconstruction assignment of variables

Source: Internet
Author: User

the destructor assignment of a variable: ES6 allows you to extract values from arrays and objects in a certain pattern, assigning values to variables, which is called deconstruction (destructuring).

The previous wording:

var a = 1;var B = 2;

ES6 allowable wording:

Let [A, b] = [+];

General Purpose:

1. Value of exchange variable

[x, Y] = [y,x];

2. function returns multiple values

Function F1 () {return [+];} var [a,b,c] = F1 ();//Return JSON object: function F1 () {return {id:1,name: "Xiaoming"};} var {id,name} = f1 ();

3, the function of the order-free definition

Function F1 ({a,b,c}) {//...} F1 ({a=1,c=3,b=2});

4. Default values for parameters

Function F1 (a=1,b=2) {//...}

5. Traverse Map Deconstruction

var map = new map (), Map.set ("First": "Hello"), Map.set ("second": "World"),//Get key and Valuefor (let [key,value] of map) {//...} Get only keyfor (let [key] of map) {//...}

ES6 Sharing--the deconstruction assignment of variables

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.