Home > Web Front-end > JS Tutorial > A brief discussion on object-oriented deep copy and shallow copy in JavaScript

A brief discussion on object-oriented deep copy and shallow copy in JavaScript

高洛峰
Release: 2017-01-03 16:01:00
Original
939 people have browsed it

Before understanding deep copy and shallow copy, you need to understand some basic concepts. The variable types stored in memory are divided into value types and reference types.

1. The storage characteristics of value type assignment, copy all the data in the variable and store it in the new variable.

For example: var num = 123; var num1=num;

means that the number stored in the variable is 123. Then copy the data, that is, copy 123. Then there are 2 arrays in the memory; assign the copied data to num2, which is characterized by having two copies of the data in the memory. This can be understood as a shallow copy.

2. Assignment of reference types.

var o={name:'Zhang San'};

var obj=o;

Assignment is to copy the data stored in variable o, and then This data is assigned to obj. There is 1 point of data in the memory, and the name attribute modified using obj will affect the name in o.

If when copying, all reference structures of the data are copied, then the data is a deep copy in the memory independently;

If when copying, only the attributes of the current object are copied Copy, and the attribute is a reference type, which is not considered, then it is a shallow copy;

Copy: Make a copy. Refers to copying the object data;

Be sure to discuss deep copy and shallow copy Make sure that the properties of the object are also reference types.

The above article briefly discussing object-oriented deep copy and shallow copy in JavaScript is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles on object-oriented deep copy and shallow copy in JavaScript, please pay attention to the PHP Chinese website!

Related labels:
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