The structure assignment of the variable of JS ES6

Source: Internet
Author: User

Variable structure assigns a lot of users

1. Value of exchange variable

Let x = 1= 2= [Y,x]

The above code swaps the values of the variable x and the variable y, so that it is not only concise, easy to read, and semantically clear.

2. Returning multiple values from a function

The function can only return a value, and if you want to return multiple values, you can only say that they are returned in an array or object. Understanding the Deconstruction assignment , it is very convenient to take the value of these values

// returns an array function example () {    return [n/A = example ();
[A,b,c]; [A]

// returns an object function example () {    return  {        foo:1,        bar:2    = example ();
foo; 1
Bar; 2

3. Definition of function parameters

The deconstruction assignment can be conveniently speaking a set of parameters corresponding to the variable name.

// A parameter is a set of sequential values function f ([x, Y, z]) {    console.log (x, y, z);} f ([+)];  The

// parameter is a set of no-order values function func ({x, y, z}) {    console.log (x, y, z);} Func ({z:3,y:2,x:1}); The

4. Extracting JSON data

Deconstructed assignments are especially useful for extracting data from JSON objects

Let Jsondata = {    ID:,    Status:"OK",    data:[123,456]             = Jsondata;console.log (id,status,number);   "OK" (2) [123, 456]

5. Default value of function parameter

、、、

6. Traverse the map structure

Any object that has the iterator interface deployed can use the for ... of loop traversal. The map structure natively supports the iterator interface, and it is very convenient to get the name and key value with the solution assignment of the variable.

var New Map (); Map.set (' first ', ' Hello '); Map.set (' second ', ' World ') ;  for (let [key,value] of map) {    console.log (key,value);}

First Hello
Second World

If you just want to get the key name, or just want to get the key value, you can write this.

// Get Key Name  for (let [key] of map) {    console.log (key);}

First
Second

// Get key value  for (let [, value] of map) {    Console.log (value);}
Hello
World

7, the input module specified method

When loading a module, it is often necessary to specify the input method. The deconstruction assignment makes the input statement very clear.

const {A, b} = require (' Source-map ');

The structure assignment of the variable of JS ES6

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.