JavaScript------deconstructed Assignment

Source: Internet
Author: User

Deconstruction Assignment

Starting with ES6, JavaScript introduces an understanding of construct assignments, which can be assigned to a set of variables at the same time.

What is a deconstructed assignment? Let's take a look at the traditional way of assigning the elements of an array to several variables individually:

var array = [‘hello‘, ‘JavaScript‘, ‘ES6‘];var x = array[0];var y = array[1];var z = array[2];

Now, in ES6, you can assign values directly to multiple variables using a deconstructed assignment:

Note that when an array element is deconstructed and assigned, multiple variables are [...] enclosed.

If the array itself is nested, you can also use the following form to deconstruct the assignment, noting that the nesting level and position should be consistent:

let [x, [y, z]] = [‘hello‘, [‘JavaScript‘, ‘ES6‘]];x; // ‘hello‘y; // ‘JavaScript‘z; // ‘ES6‘

Deconstruction assignments can also omit certain elements:

let [, , z] = [‘hello‘, ‘JavaScript‘, ‘ES6‘]; // 忽略前两个元素,只对z赋值第三个元素z; // ‘ES6‘

If you need to remove several properties from an object, you can also use a deconstructed assignment to quickly get the specified properties of an object:

JavaScript------deconstructed 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.