Home > Web Front-end > JS Tutorial > What Do Curly Brackets ( {... } = ... ) in Destructuring Assignment Statements Represent?

What Do Curly Brackets ( {... } = ... ) in Destructuring Assignment Statements Represent?

DDD
Release: 2024-10-21 06:52:02
Original
1069 people have browsed it

What Do Curly Brackets ( {... } = ... ) in Destructuring Assignment Statements Represent?

What Does the Curly Brackets in var { ... } = ... Statements Represent?

Destructuring assignment, signified by the curly brackets in var { ... } = ... statements, is a pattern matching feature in JavaScript akin to that found in languages like Haskell. It provides a succinct way to extract and assign values from objects and arrays.

For Objects:

Let's consider the following example:

<code class="javascript">var ascii = {
    a: 97,
    b: 98,
    c: 99
};

var {a, b, c} = ascii;</code>
Copy after login

This statement extracts the a, b, and c properties from the ascii object and assigns them to the corresponding variables. It is equivalent to the following code:

<code class="javascript">var a = ascii.a;
var b = ascii.b;
var c = ascii.c;</code>
Copy after login

For Arrays:

Similar destructuring can be performed on arrays:

<code class="javascript">var ascii = [97, 98, 99];

var [a, b, c] = ascii;</code>
Copy after login

This code extracts and assigns the first, second, and third elements of the ascii array to a, b, and c, respectively. It is equivalent to:

<code class="javascript">var a = ascii[0];
var b = ascii[1];
var c = ascii[2];</code>
Copy after login

Property Renaming:

Destructuring assignment also allows you to extract and rename a property:

<code class="javascript">var ascii = {
    a: 97,
    b: 98,
    c: 99
};

var {a: A, b: B, c: C} = ascii;</code>
Copy after login

This code assigns the a, b, and c properties to variables A, B, and C, respectively.

The above is the detailed content of What Do Curly Brackets ( {... } = ... ) in Destructuring Assignment Statements Represent?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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