Introduction to object-oriented deep copy and shallow copy in JavaScript

Source: Internet
Author: User
The following small series will bring you an article on object-oriented deep copy and shallow copy in JavaScript. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the basic concepts that need to be understood before deep copy and shallow copy. The types of variables stored in the memory are divided into value type and reference type.

1. storage features of Value Type assignment. Copy all the data in the variable and store it to the new variable.

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

The number stored in the variable is 123. Then copy the data, that is, copy 123. there are two arrays in the memory, and the copy data is assigned to num2, which features two data copies in the memory. this can be understood as a light copy.

2. assign values to the reference type.

Var o = {name: 'zhangsan '};

Var obj = o;

The value assignment is to copy the data stored in variable o and assign the value to obj. The memory contains 1 point of data. The name Attribute Modified using obj will affect the name in o.

If all the referenced structures of the data are copied in one copy, the data is copied independently in the memory;

If only the attributes of the current object are copied, and the attribute is of the reference type, it is a shortest copy;

Copy: Copy object data;

When discussing deep copy and shallow copy, make sure that the attributes of the object are also of the reference type.

In the above discussion, the object-oriented deep copy and shallow copy in JavaScript are all the content shared by the editor. I hope to give you a reference and support for PHP.

For more information about object-oriented deep copy and shortest copy in JavaScript, refer to PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.