The use of the ES6 variable to deconstruct the assignment

Source: Internet
Author: User

The following is sorted out from the Nanyi of "ECMAScript 6 ," chapter III the use of the assignment of the value of the deconstruction of the variables array

Let [A, B, c] = [1, 2, 3];
let [foo = true] = [];
Object
Let {foo, bar} = {foo: "AAA", Bar: "BBB"};
let {Foo:baz} = {foo: ' aaa ', bar: ' BBB '};
String
const [A, B, C, D, e] = ' Hello ';
Function
function Add ([x, Y]) {return
    x + y;
}
Add ([1, 2]); 3
[[1, 2], [3, 4]].map ([A, b]) => A + B);
function Move ({x = 0, y = 0} = {}) {return
    [x, y];
}
Move ({x:3, y:8}); [3, 8]
The purpose of the deconstruction assignmentValue of the Exchange variable
Let x = 1;
Let y = 2;
[x, Y] = [y, x];
Returning multiple values from a function
function Example () {return
    [1, 2, 3];
}
Let [A, B, c] = example ();
Function app () {return
    {
        foo:1,
        bar:2
    };
}
Let {foo, bar} = App ();
Definition of function parameters
function f ([x, Y, z]) {...}
f ([1, 2, 3]);

function g ({x, y, z}) {...}
G ({z:3, y:2, x:1});
Extract JSON data
Let Jsondata = {
    id:42,
    status: ' OK ',
    data: [867, 5309]
};
Let {ID, status, data:number} = Jsondata;
Default values for function arguments
Jquery.ajax = function (URL, {
    async = true,
    beforesend = function () {},
    cache = True,
    complete = funct Ion () {},
    Crossdomain = False,
    global = True,
    //... more config
}) {
    //... do stuff
};
Traversing the map structure
var map = new map ();
Map.set (' A ', ' hello ');
Map.set (' Second ', ' World ');

for (let [key, value] of map) {
    Console.log (key + ' is ' + value);
}
for (let [key] of map) {
    console.log (key);
}
for (let [, value] of map) {
    Console.log (value);
}
Input module's specified method
const {Sourceconsumer, Sourcenode} = require (' Source-map ');

The above content is sorted out from the Nanyi "ECMAScript 6 Getting Started" the third chapter variable's deconstruction assignment

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.