What does es6 destructuring assignment mean?

WBOY
Release: 2022-04-01 11:00:04
Original
2492 people have browsed it

In es6, destructuring assignment means performing pattern matching on an array or object, and then assigning values ​​to the variables in it; destructuring assignment is also an extension of the assignment operator, and the syntax is "let destructuring target = destructuring source;".

What does es6 destructuring assignment mean?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

es6 What does destructuring assignment mean

Overview

Destructuring assignment is an extension of the assignment operator.

It is a pattern matching for an array or object, and then assigns values ​​to the variables in it.

The code writing is concise and easy to read, and the semantics are clearer; it also facilitates the acquisition of data fields in complex objects.

Deconstruction model

In deconstruction, the following two parts are involved:

  • The source of deconstruction, the deconstruction assignment expression the right part of.

  • The goal of destructuring is to deconstruct the left part of the assignment expression.

Destructuring of array model (Array)

Basic

let [a, b, c] = [1, 2, 3];
// a = 1
// b = 2
// c = 3
Copy after login

Can be nested

let [a, [[b], c]] = [1, [[2], 3]];
// a = 1
// b = 2
// c = 3
Copy after login

Can Ignore

let [a, , b] = [1, 2, 3];
// a = 1
// b = 3
Copy after login

Incomplete destructuring

let [a = 1, b] = []; // a = 1, b = undefined
Copy after login

Remaining operators

let [a, ...b] = [1, 2, 3];
//a = 1
//b = [2, 3]
Copy after login

Examples are as follows:

What does es6 destructuring assignment mean?

What does es6 destructuring assignment mean?

Note:

  • The array structures on the left and right sides of the assignment need to be consistent. This is called "pattern matching"

  • If the number of variables on the left and right sides does not match the number of values, then skip the missing part directly (remember, if the left one is skipped, the corresponding position on the right side must also be skipped)

  • If there are three dots before a variable on the left, it means that all the values ​​at the corresponding position on the right and thereafter will be combined into an array and assigned to the variable on the left, and the left must be Only the last variable can be preceded by three dots, otherwise an error will be reported

[Related recommendations: javascript video tutorial, web front-end]

The above is the detailed content of What does es6 destructuring assignment mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!