On several cloning (clone) methods in JavaScript

Source: Internet
Author: User
Tags hasownproperty

Reprint: https://www.cnblogs.com/zouhao/p/7278117.html

A: In JavaScript, if the cloned object is a basic type, we can assign it directly:

1 var sStr = "Kingwell"; 2 var cStr = sstr;3 alert (cStr);//output KINGWELL4 SSTR = "ABC"; 5 alert (CSTR);//output Kingwell;

When you assign a value to another variable, the value of that variable is not affected when it is changed.

Two: If it's not the basic type, it's all different:

1 var aarr = [0,1,2,3];2 var m = aarrr;3 alert (m);//Output 1,2,34 aarr=[3,2,1,0];5 alert (m);//output 3,2,1,0; This value is changed because M is just a reference to Aarr, If the value of Aarr is changed, then M will change accordingly.

If we want to clone an array, the simplest way:

1 var aarr = [0,1,2,3];2 var m = aarr.slice (0); 3 Aarr = [3,2,1,0];4 alert (m);//output 0,1,2,3, while the value in Aarr has changed, but because the slice method has been created A new array.

We can create a function to clone all objects:

1 function Clone (obj) {2     var o; 3     if (typeof obj = = "Object") {4         if (obj = = = null) {5             o = null; 6         } else {7             if (obj instanceof Array) {8                 o = []; 9                 for (var i = 0, len = obj.length; i < Len; i++) {                     O.push (Clone (Obj[i))),                 }12             } else {                 16 o = {};14 for                 (var j in obj) {o[j]                     = Clone (Obj[j]);                 }17             }18         }19     } else {         o = obj;21     }22     return o;23}

Three: node cloning:

1 var p = document.getelementsbytagname ("P") [0];2 var cp = P.clonenode ();//Clone P node 3 var cp = P.clonenode (TRUE);//clone P node, deep clone, Clones the node and the child content underneath the node.

--------------------------------------------------------------------------------------------------------------- ---

Source: http://www.cnblogs.com/snowinmay/p/3853425.html

There is no direct way to provide object cloning in JavaScript. So when you change object B in the code below, it changes the object A.

{K1:1, K2:2, K3:3}= a; b4;      

If you want to change only B and keep a constant, you need to copy object A.

Object copying with jquery

In the case of jquery, jquery's own extend method can be used to implement object replication.

{K1:1, K2:2, K3:3}{}; $.  Extend(b, a);               
Customizing the Clone () method to implement object replication

The following method is the basic idea of object replication.

Object.Prototype. clone=function(){var copy=(ThisinstanceofArray)?[]:{};For(attrInchThis){If(!obj.hasOwnProperty(attr))Continue; Copy[attr]=(typeofThis[I]=="Object"[attr]. Clone (): Obj[attr;  return copy;} ;a = {k1:1:2 :3};b = a. Clone            

The following example is considered more comprehensive and applies to deep copy of most objects.

functionClone(obj){Handle the 3 simple types, and null or undefinedIf(Null= = obj||"Object"!=typeof obj)return obj;Handle DateIf(objinstanceofDate){var copy=NewDate(); Copy.SetTime(obj.GetTime());return copy;}Handle ArrayIf(objinstanceofArray){var copy=[];For(var i=0,var len= obj. length; I< Len;++i){Copy[I]=Clone(obj[I]);}return copy;}Handle ObjectIf(objinstanceofObject){var copy={};For(var attrIn obj) { if (obj.  hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy obj! Its type isn ' t supported. ); }< /c6> 
Classification:Java Script

On several cloning (clone) methods in JavaScript

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.