On the object-oriented deep copy and shallow copy _javascript techniques in JavaScript

Source: Internet
Author: User
Tags shallow copy

Before you can understand deep and shallow copies, you need to understand some basic concepts, and the types of variables stored in memory are classified as value types and reference types.

1, the value type of the storage characteristics of the variable, the data in all copies of a copy, stored to the new variable.

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

Indicates that the number stored in the variable is 123. Then copy the data and copy the 123 copies. Then there are 2 arrays in memory, and the copy data is assigned to NUM2, which is characterized by two copies of data in memory. This can be understood as a shallow copy.

2. Assignment of reference type.

var o={name: ' John '};

var obj=o;

The assignment is to copy the data stored in the variable o and assign the data to obj. There are 1 minutes of data in memory, and the Name property modified by obj affects name in O.

If the copy of the data, all the reference structure of the copy, then the data in memory Independent is a deep copy;

If a copy is made, only the properties of the current object are copied, and the attribute is a reference type, which is not considered, then it is a shallow copy;

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

When discussing the handcuffs and the cuff, be sure to ensure that the object's properties are also reference types.

This article on JavaScript in the object-oriented deep copy and shallow copy is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.