In JavaScript Many people copy an object directly with the "=", because everyone thinks the scripting language is no pointers, references, addresses and so on, so directly with "=" can be used to copy an object to another object, the following code:
var i1 = 1; var i2 = i1; I2 = 2; Alert ("I1: +i1+", I2: "+i2");
Output result: I1:1, I2:2
However, it may not be found that the "copy" used in the object type is "wrong" because it simply copies the address of the object, so the following code will replicate "error":
Code 2
var o1 = {i:1,s: "O1"}; var o2 = O1; o2.i=2; O2.s= "O2"; Alert ("o1.i:" +o1.i+ ", o2.i:" +o2.i); Alert ("O1.S: +o1.s+", O2.s: "+o2.s");
Output: O1.i:2, O2.i:2
O1.s:o2, O2.s:o2
You may ask why code 1 can be copied, but code 2 is not copied?
In fact, the "basic data type" can be copied, and non-basic types (including strings) only copy their references. Why did you do it? The reason is simple, in order to reduce overhead. We all know that non-basic types are sometimes very large, and if you re-open up memory to hold such a large object, the overhead is very high, which results in a slow run. The scripting language is run directly in the virtual machine (or browser), it passes through the virtual machine layer to process the code, the speed has been much slower than other compiler language, so if the non-basic objects to do "copy", then you may have to wait for a year to run the program, so you can only copy the object reference.
But many times we do not want the function to modify our object parameters, which requires the use of the object's clone, we should make a clone of the object, and then manipulate the cloned object, so that it does not affect our original object. If you need to copy the entire object, you must have a property or method reference copy a partial, so that for each property to open up memory to hold the data you need, of course, this will be relatively slow, especially when the volume of data is very large, there is no object cloning function in JS, so we need to implement, the implementation method is not complex, Basically do some property copy, I found some on the internet, but some implementations are not good, such as the array object is cloned into a JSON object, and does not retain the original array. But finally found a good cloning function, the perfect implementation of the JS object cloning function, whether it is an array object or an ordinary object, can be very good cloning, this function using constructor (function constructor) for replication.
Method One:
Object.prototype.Clone =function(){ varObjclone; if( This. constructor = =Object) {Objclone=New This. Constructor (); }Else{Objclone=New This. Constructor ( This. ValueOf ()); } for(varKeyinch This){ if(Objclone[key]! = This[key]) { if(typeof( This[Key]) = = ' object ') {Objclone[key]= This[key]. Clone (); }Else{Objclone[key]= This[key]; } }} objclone.tostring= This. toString; Objclone.valueof= This. valueOf; returnObjclone;}
Method Two:
functionClone (obj) {varo; Switch(typeofobj) { Case' Undefined ': Break; Case' string ': o = obj + '; Break; Case' number ': o = obj-0; Break; Case' Boolean ': o = obj; Break; Case' Object ' : if(obj = = =NULL) {o=NULL; }Else{ if(objinstanceofArray) {o= []; for(vari = 0, len = obj.length; i < Len; i++) {O.push (Clone (Obj[i))); } }Else{o= {}; for(varKinchobj) {O[k]=Clone (Obj[k]); } } } Break; default: o= obj; Break; } returno; }
Method Three:
function Clone3 (obj) { function Clone () {} = obj; var New Clone (); for (var in o) { if(typeof O[a] = = "Object") { = clone3 (o[a]); } } return o; }
Method Four:
arrayobj=[1,2,3,4,5];
// returns a copy array of the array, note that it is a new array, not a pointer to the // Returns a copy array of the array, note that it is a new array, not a pointer to the