JavaScript Deep Replication

Source: Internet
Author: User
Tags shallow copy

In the work encountered the problem of the depth of the copy, so detailed summary:

Deep copy and shallow copy are only for complex objects such as Object, Array. Simply put, a shallow copy replicates only the properties of one layer of objects, while a deep copy recursively replicates all levels.

 var  obj = {a:1, arr: [2,3 var  shadowobj = shadowcopy (obj);  function   shadowcopy (src) { var  DST = {};  for  (var  prop in   src) { if   (Src.hasownproperty (prop)) {Dst[prop]  = Src[prop]; }}  return   DST;} 

This is a typical shallow copy, where the Shadowcopy method copies the properties of an object sequentially, does not replicate recursively, and the JavaScript storage objects are stored addresses, so shallow copy causes Obj.arr and Shadowobj.arr to point to the same memory address. When one of the variables modifies the value pointed to, the other variable is modified at the time of the call.

SHADOWOBJ.ARR[1] = 5; obj.arr[1]   //  = 5

Here's the code for the deep copy:

functiondeepcopy (obj) {varTMP = {};  for(varKinchobj) {tmp[K]=obj[K]; }         returntmp; }    //one of the most critical steps in this function tmp[K] = obj[K]    //So here we just need to make sure obj[K] This assignment is a deep copy of the object.    //Note: The purpose of the function is to obtain a deep copy of obj. So recursion.    functiondeepcopy (obj) {varTMP ={}, K;  for(kinchobj) {                if(typeofobj[K] = = = ' object ') {tmp[K]=deepcopy (obj[K]); } Else{tmp[K]=obj[K]; }            }            returntmp; }        //If you work with this object        varO3 ={name:' Jim ', scores: [90,                 95,                85            ]        }; //The code cannot handle the case of the array, make the following changes        functiondeepcopy (obj) {varTMP = obj.length >= 0?objinstanceofArray? []: {length:0}: {}, K;  for(kinchobj) {                if(typeofobj[K] = = = ' object ') {tmp[K]=deepcopy (obj[K]); } Else{tmp[K]=obj[K]; }            }            returntmp; }

For deep replication, deep replication can cause performance problems if the objects are larger and have more layers. When encountering scenarios where deep replication is required, there are other alternatives to consider. In the actual application scenario, it is also more commonly used for shallow replication.

andJSON.parse( JSON.stringify(a) )这种方法比较简单,但同时也存在问题

    • Unable to copy function
    • The prototype chain is gone, objects are object, and the class that belongs is gone. This discards the object's constructor, that is, after a deep copy, no matter what the object's original constructor is, it becomes object after the deep copy. In addition, objects such as RegExp cannot be deeply copied in this way.

JavaScript Deep Replication

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.