Home Web Front-end JS Tutorial What is the difference between deep copy and shallow copy in JS

What is the difference between deep copy and shallow copy in JS

Feb 18, 2024 am 10:26 AM
js Shallow copy deep copy

What is the difference between deep copy and shallow copy in JS

What is the difference between deep copy and shallow copy in JS? Specific code examples are needed

In JavaScript, the copy of an object is divided into two types: shallow copy and deep copy. Way. A shallow copy only copies the reference address of an object, while a deep copy creates a completely independent copy.

Shallow copy is to copy the reference address of the original object to the new object, and they point to the same memory space. When the properties of any one of the objects are modified, the corresponding properties of the other object will also be modified. This is because they share the same memory address.

Deep copy is to create a brand new object and copy all the attributes in the original object to the new object one by one. The new object and the original object have no influence on each other. Even if you modify the properties of one object, the properties of the other object will not be affected.

Below, I will illustrate the difference between shallow copy and deep copy through specific code examples.

First, let’s look at an example of shallow copy:

let obj1 = {name: "Alice", age: 20};
let obj2 = obj1;

obj1.age = 21; 

console.log(obj1); // {name: "Alice", age: 21}
console.log(obj2); // {name: "Alice", age: 21}
Copy after login

In the above code, we implement it by assigning obj1 to obj2 A shallow copy. When the age attribute of obj1 is modified, the age attribute of obj2 is also modified because they point to the same memory address. .

Next, let’s look at an example of deep copy:

function deepClone(obj) {
    if (obj === null || typeof obj !== 'object') {
        return obj;
    }
    
    let clone = Array.isArray(obj) ? [] : {};
    
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            clone[key] = deepClone(obj[key]);
        }
    }
    
    return clone;
}

let obj1 = {name: "Alice", age: 20};
let obj2 = deepClone(obj1);

obj1.age = 21;

console.log(obj1); // {name: "Alice", age: 21}
console.log(obj2); // {name: "Alice", age: 20}
Copy after login

In the above code, we define a deepClone function to implement deep copy. This function first determines whether the incoming parameter is null or not an object type. If so, it returns directly, otherwise it creates an empty object clone of the same type as the incoming object. Then by traversing the properties of the original object, recursively calling the deepClone function to deeply copy each property, and assign it to the corresponding clone property. Finally, the new object clone is returned.

By using the deepClone function, we implement a deep copy of obj1. Even if the age attribute of obj1 is modified, the age attribute of obj2 remains unchanged. This is because they are two complete independent object.

To sum up, shallow copy only copies the reference address of the object, while deep copy creates a completely independent copy. Deep copy can ensure that the original object will not be affected when modifying the copy object, and is suitable for copying objects with nested structures. It should be noted that in actual development, deep copy may cause large performance overhead, so it is necessary to choose an appropriate copy method based on the actual situation.

The above is the detailed content of What is the difference between deep copy and shallow copy in JS. 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)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Maps to implement map pan function

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

How to use JS and Baidu Map to implement map click event processing function

How to use JS and Baidu Maps to implement map heat map function How to use JS and Baidu Maps to implement map heat map function Nov 21, 2023 am 09:33 AM

How to use JS and Baidu Maps to implement map heat map function

See all articles