Table of Contents
Is es6 destructuring a deep copy?
Home Web Front-end Front-end Q&A Is es6 deconstruction a deep copy?

Is es6 deconstruction a deep copy?

Apr 25, 2022 pm 07:04 PM
es6

es6 deconstruction is not a deep copy. If the original object of deconstruction is a one-dimensional array or object, then destructuring is a deep copy. If the original object of deconstruction is a multi-dimensional array or object, then destructuring is a shallow copy. Because destructuring cannot achieve the effect of deep copy for multi-dimensional arrays, destructuring cannot It is considered a deep copy, but should be a shallow copy.

Is es6 deconstruction a deep copy?

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

Is es6 destructuring a deep copy?

Everyone must be familiar with the knowledge about destructuring assignment in ES6. The so-called destructuring assignment is a solution that uses a pattern to quickly retrieve data from the target structure, for example:
Is es6 deconstruction a deep copy?
Through destructuring, we can easily retrieve the data we want from arrays and objects. . However, you may ask, what does this have to do with deep copy and shallow copy?

Let’s first review the deep copy and shallow copy.

The so-called shallow copy and deep copy: Shallow copy means that during the copy process, the part during traversal is of object/array type Points to the original address, while deep copy completely opens up a new memory address. In other words,

  • deep copy: modifying the value of the new variable will not affect the value of the original variable. By default, basic data types (number, string, null, undefined, boolean) are deep copied.
  • Shallow copy: Modifying the value of the new variable will affect the value of the original variable. By default, reference types (object) are shallow copies.

So, in deconstruction, is it a deep copy or a shallow copy?

Is es6 deconstruction a deep copy?
In the above code, we deconstruct the array and object respectively, then modify the value of the destructured variable, and then print the variable and the original array and object respectively. The results are as follows:

Is es6 deconstruction a deep copy?
We found that the data of the original array and object have not changed. Does this mean that the destructuring assignment is a deep copy? ?

Let’s slightly modify the example, the code is as follows:

Is es6 deconstruction a deep copy?
We added a new data attribute to the object, the data of data is an array, and then after destructuring, we The data of data was modified, and the result is as follows:

Is es6 deconstruction a deep copy?
We found that the change of the deconstructed variable data caused the change of the original data, that is, the destructured assignment is still a shallow copy.

To summarize:

Destructuring assignment, if the original object being deconstructed is a one-dimensional array or object, its essence is to perform an equal sign assignment on the basic data type, then it is a deep Copy;
If it is a multi-dimensional array or object, its essence is to assign equal signs to reference type data, then it is a shallow copy;
The final conclusion is: destructuring assignment is a shallow copy (because it cannot do multi-dimensional Array or object achieves the function of deep copy);

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

The above is the detailed content of Is es6 deconstruction a deep copy?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is async for es6 or es7? Is async for es6 or es7? Jan 29, 2023 pm 05:36 PM

Is async for es6 or es7?

How to reverse an array in ES6 How to reverse an array in ES6 Oct 26, 2022 pm 06:19 PM

How to reverse an array in ES6

How to find different items in two arrays in es6 How to find different items in two arrays in es6 Nov 01, 2022 pm 06:07 PM

How to find different items in two arrays in es6

Why does the mini program need to convert es6 to es5? Why does the mini program need to convert es6 to es5? Nov 21, 2022 pm 06:15 PM

Why does the mini program need to convert es6 to es5?

Is require an es6 syntax? Is require an es6 syntax? Oct 21, 2022 pm 04:09 PM

Is require an es6 syntax?

What does es6 temporary Zenless Zone Zero mean? What does es6 temporary Zenless Zone Zero mean? Jan 03, 2023 pm 03:56 PM

What does es6 temporary Zenless Zone Zero mean?

Is es2015 the same as es6? Is es2015 the same as es6? Oct 25, 2022 pm 06:51 PM

Is es2015 the same as es6?

Is es6 map ordered? Is es6 map ordered? Nov 03, 2022 pm 07:05 PM

Is es6 map ordered?

See all articles