Home Web Front-end JS Tutorial Discussion on js deep copy examples

Discussion on js deep copy examples

Mar 06, 2018 pm 02:15 PM
javascript Example Discuss

Deep copy is a copy that copies the parent object to the child object, and the memory and subsequent operations of the two do not affect each other. This article mainly shares with you the discussion of js deep copy examples, hoping to help everyone.

(1) Method 1

1

2

3

4

5

6

7

8

9

10

function copy(obj1,obj2){

  var obj2=obj2||{};

  for(var name in obj1){    if(typeof obj1[name] === "object"){ //先判断一下obj[name]是不是一个对象

      obj2[name]= (obj1[name].constructor===Array)?[]:{};

      copy(obj1[name],obj2[name]); //然后来无限递归

    }else{

      obj2[name]=obj1[name];  //如果不是对象,直接赋值。

    }

  return obj2;

}

Copy after login
Copy after login

Usage method:

1

2

3

4

5

6

var obj1 = {

    se:[{a:1,b:2},{c:3}],

    sa:{a:"g"},

    sc:function(){console.log(1)}

}var n_obj = copy(obj1,{})

console.log(n_obj)

Copy after login
Copy after login

(2) Method 2

1

2

3

function d_clone(obj) {

    return Object.getPrototypeOf(Object.create(obj));

}

Copy after login
Copy after login

Usage method:

1

2

3

4

5

6

var obj1 = {

    se:[{a:1,b:2},{c:3}],

    sa:{a:"g"},

    sc:function(){console.log(1)}

}var n2_obj = d_clone(obj1);

console.log(n2_obj)

Copy after login
Copy after login

(3) Method 3

1

JSON.parse(JSON.stringify(obj)

Copy after login
Copy after login

Description: The attributes of obj cannot contain functions.

(1) Method 1

1

2

3

4

5

6

7

8

9

10

function copy(obj1,obj2){

  var obj2=obj2||{};

  for(var name in obj1){    if(typeof obj1[name] === "object"){ //先判断一下obj[name]是不是一个对象

      obj2[name]= (obj1[name].constructor===Array)?[]:{};

      copy(obj1[name],obj2[name]); //然后来无限递归

    }else{

      obj2[name]=obj1[name];  //如果不是对象,直接赋值。

    }

  return obj2;

}

Copy after login
Copy after login

Usage method:

1

2

3

4

5

6

var obj1 = {

    se:[{a:1,b:2},{c:3}],

    sa:{a:"g"},

    sc:function(){console.log(1)}

}var n_obj = copy(obj1,{})

console.log(n_obj)

Copy after login
Copy after login

(2) Method 2

1

2

3

function d_clone(obj) {

    return Object.getPrototypeOf(Object.create(obj));

}

Copy after login
Copy after login

Usage method:

1

2

3

4

5

6

var obj1 = {

    se:[{a:1,b:2},{c:3}],

    sa:{a:"g"},

    sc:function(){console.log(1)}

}var n2_obj = d_clone(obj1);

console.log(n2_obj)

Copy after login
Copy after login

(3) Method 3

1

JSON.parse(JSON.stringify(obj)

Copy after login
Copy after login

Description: The attributes of obj cannot contain functions.

Related recommendations:

Related recommendations:

In-depth understanding of JavaScript deep copy performance

What is js deep copy And shallow copy and its implementation

The difference between JavaScript shallow copy and deep copy

The above is the detailed content of Discussion on js deep copy examples. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Learn best practice examples of pointer conversion in Golang Learn best practice examples of pointer conversion in Golang Feb 24, 2024 pm 03:51 PM

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Deep dive: What is the Django framework? Deep dive: What is the Django framework? Jan 19, 2024 am 09:23 AM

The Django framework is a Python framework for web applications that provides a simple and powerful way to create web applications. In fact, Django has become one of the most popular Python web development frameworks and has become the first choice for many companies, including Instagram and Pinterest. This article will delve into what the Django framework is, including basic concepts and important components, as well as specific code examples. Django basic conceptsDjan

The relationship between the number of Oracle instances and database performance The relationship between the number of Oracle instances and database performance Mar 08, 2024 am 09:27 AM

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.

How to implement an online electronic signature system using WebSocket and JavaScript How to implement an online electronic signature system using WebSocket and JavaScript Dec 18, 2023 pm 03:09 PM

Overview of how to use WebSocket and JavaScript to implement an online electronic signature system: With the advent of the digital age, electronic signatures are widely used in various industries to replace traditional paper signatures. As a full-duplex communication protocol, WebSocket can perform real-time two-way data transmission with the server. Combined with JavaScript, an online electronic signature system can be implemented. This article will introduce how to use WebSocket and JavaScript to develop a simple online

Deep dive: Single-threaded features in Go language Deep dive: Single-threaded features in Go language Mar 15, 2024 pm 02:09 PM

As a modern programming language, Go language has been loved and favored by more and more developers in recent years due to its simplicity and efficiency. One of the unique features is its single-threaded nature. In traditional multi-threaded programming languages, developers usually need to manually manage synchronization and mutual exclusion between threads. In Go language, with its unique coroutine (Goroutine) and communication mechanism (channel), it can be convenient and efficient. implement concurrent programming. 1. Goroutine and single thread: Go language

A deep dive into common special characters in Linux A deep dive into common special characters in Linux Mar 14, 2024 pm 02:54 PM

As a commonly used open source operating system, Linux operating system has strong customizability and flexibility. When using Linux systems, we often encounter the processing of various special characters. These special characters have special meanings in the command line and can implement many advanced functions. This article will delve into the common special characters in Linux and introduce their usage in detail with specific code examples. Wildcards: Wildcards are special characters used to match file names. Common wildcards include *,?, [], etc. Here are several

See all articles