What is "clone"?
In the actual programming process, we often encounter this situation: there is an object A, in a moment a has already included some valid values, you may need one and a exactly the same new object B, and any subsequent changes to B will not affect the value of a, that is, A and B are two separate objects, But the initial value of B is determined by the A object. In the Java/javasript language, this requirement is not satisfied with simple assignment statements. There are many ways to meet this requirement, but implementing the Clone () method is one of the simplest and most efficient means, and of course there is no such method in the JavaScript language.
So I specifically wrote two cloning methods: one for shallow copy, one for deep replication.
Explain:
Shallow copy (Shadow clone): Only the base type of the object is copied, the object type, and still belongs to the original reference.
Deep copy (Depth clone): The base class of the object is not tightly copied, and the objects in the original object are copied. That is, it is entirely a new object.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 "> <meta name=" generator "content=" Wawa Editor 1.0 "> <meta name=" Author "content=" Eight Gods Yan "> &l T META name= "Keywords" content= "javascript,java,xml,xslt,asp,vbscript,asp. Net,c#,c++,database" > <meta NAME= " Description "content=" failed programmers, ubiquitous "> </HEAD> <BODY> <script language=" JavaScript "> <!--Fun Ction Object.prototype.clone () {var newObj = new Object (); For (elements in this) {newobj[elements] = this[elements]; return NEWOBJ; function Object.prototype.cloneAll () {function Cloneprototype () {} Cloneprototype.prototype = this; var obj = new Cloneprototype (); for (var ele in obj) {if (typeof (Obj[ele)) = = "Object") Obj[ele] = Obj[ele].cloneall (); return obj; } var obj1 = new Object (); Obj1. Team = "a"; Obj1. Powers = new Array ("Iori", "Kyo"); Obj1.msg = function () {alert ()}; Obj1.winner = new Object (); Obj1.winner.name = Obj1. Powers[0]; Obj1.winner.age = 23; Obj1.winner.from = "Japan" var obj1_clone = Obj1.cloneall (); Obj1_clone. Team = "Second"; Obj1_clone. Powers = new Array ("Jimmy", "Anndy"); Obj1_clone.winner.name = Obj1_clone. POWERS[1]; Obj1_clone.winner.age = 22; Obj1_clone.winner.from = "USA"; msg = "2003 Circles Boxing King Singles Cup, the King of the fight:" \ n A group of war form: \ n "msg + + obj1." team+ "Group, Personnel list:" +obj1. Powers+ "\ n"; MSG + + After the first round, the winner is: "+obj1.winner.name +", the contestants Age: "+obj1.winner.age+", from the island: "+obj1.winner.from+"; msg + = "\ n/b group of War form: \ n" msg + Obj1_clone. team+ "Group, Personnel list:" +obj1_clone. Powers+ "\ n"; msg + + "After the first round, the winner is:" +obj1_clone.winner.name + ", contestant Age:" +obj1_clone.winNer.age+ ", from the International police force:" +obj1_clone.winner.from+ "; Alert (msg); --> </SCRIPT> </BODY> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]