JavaScript depth Copy

Source: Internet
Author: User
Tags shallow copy

JavaScript has five basic data types (Undefined, NULL, Boolean, String, number), and a complex data type, which is an object.

Undefined is actually the output of a variable that has been declared but not assigned a value, and null is actually the result of a nonexistent object

  • For a simple data type
    Their values occupy a fixed amount of space in memory and are stored in the stack memory. When a variable copies the value of a base type to another variable, a copy of the value is created, and a property cannot be added to the value of the base type.

    1234 var a = 1; var b = A; a.attr = ' long '; Console.log (a.attr) //undefined

    A is a simple data type (number) in the upper code, and B is a copy of a, both of which occupy different positions but equal memory space.

  • For complex data types
    A complex data type is a reference type whose value is an object, stored in heap memory, and a variable containing a reference type value actually contains not the object itself, but a pointer to that object. Copying the value of a reference type from one variable to another is actually a pointer, so two variables end up pointing to the same object.

    12345678 var obj = { name: ' Long ', age : 0}; var obj2 = obj; obj2.c = 5; console.log (obj); //oject {name: ' Long ', age:0, c:5} Console.log (OBJ2); //oject {name: ' Long ', age:0, c:5}

We can see that when obj is assigned to OBJ2, when we change the property value of one of the objects, two objects are changed because both obj and Obj2 both point to the same pointer and the assignment is to copy the pointer, so when we change one of the values, it affects the value of the other variable.

Shallow copy

In fact, the code above is a shallow copy, sometimes we just want to back up the array, but simply let it be assigned to a variable, change one, and the other one is followed by the change, but many times this is not what we want.

12345678 var obj = { name:' Wsscat ', age :0 } var obj2 = obj; obj2[' c '] = c8>5; console.log (obj); //object {name: "Wsscat", age:0, C:5} console.log (obj2); Object {name: "Wsscat", age:0, c:5}

Deep Copy Array

For arrays we can use the slice () and concat () methods to solve the above problem.

  • Slice

    12345 v Ar arr = [ ' ge ', ' Yu ', ' long '); var arrcopy = Arr.slice (0); Arrcopy[0] = ' Gai '; console.log (arr) //[' ge ', ' yu ', ' Long '] console.log (arrcopy) //[' Gai ', ' Yu ', ' Long ']
  • Concat

    1234 var arr = [' ge ', ' Yu ', ' long ']; var arrcopy = Arr.concat (); arrcopy[0] = ' gai '; //console, just like the top .
Object

Object we can define a new object and traverse the new property to implement a deep copy
Original method:

12345678910 var obj = { name: ' Geyulong ', age : 0}; var obj2 = new Object (); Obj2.name = Obj.name;obj2.age = Obj.age;obj.name = "Gaiyulong"; console.log (obj); //Object {name: ' Geyulong ', age:0} Console.log (OBJ2); //Object {name: ' Gaiyulong ', age:0}

Of course we are going to encapsulate a method to implement a deep copy.

123456789101112 var deepcopy = function (source) { var result = {}; For (var key in source) { if (typeof source[key] = = = ' object ') { Result[key] = deepcopy (Source[key])} else { Result[key] = Source[key]}} return result; var obj2 = deepcopy (obj);

Sharecomments

JavaScript depth Copy

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.