Deep copy and shallow copy of JS

Source: Internet
Author: User

What is deep copy and shallow copy?

The concept of deep copy and shallow copy exists only on the object array and on the array obj .

  Shallow copy is: Fuzzy replication, that is, whether the other side is a string type or reference type are copied all over. As a result, the contents of the two variables change simultaneously.

  A deep copy is a purposeful copy that simply wants to copy the contents of a variable store.

Deep Copy method: letb = Json.parse (Json.strigify (a));

There are two types of JavaScript values : string Types and reference types

Copy of the string type, the value changes without affecting another variable, such as:

    Let a=7;  Let B=a; a=6;

Console.log (a)//a=6

Console.log (b)//b=7

  Copy of a reference type that affects another variable when the value changes. Such as:

   Let a={name: ' Xiaohong '};  Let B=a; A.name= ' Xiaowang;

Console.log (A.name)//a= ' Xiaowang '

Console.log (b.name)//b= ' Xiaowang '

Because arrays and objects store not the content itself, but the address of the content, the variable also points to the address when the array and object are copied to another variable.

When A.name changes only the content changes, the address does not change, so point to a at the same address. A.name and B.name have changed.

    

Deep copy and shallow copy of JS

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.