The implementation of shallow copy and deep copy in Javascript

Source: Internet
Author: User
Tags array copy reference return shallow copy variable
Javascript

The image assignment in JavaScript is the same as in Java, and is passed as a reference.
That is, when assigning an image to a variable, the variable is still pointing to the address of the original image. How do you do that? The answer is cloning.

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 cloning): Only the basic type of the image is copied, and the image type is still the original reference.
Deep copy (Depth clone): does not compact the base class of the object, but also copies the image from the original object. That is, it is entirely new.

The following is the code, I have tested, have not encountered any problems, I hope you are interested in participating together.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </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" >
<meta name= "Keywords" content= "javascript,java,xml,xslt,asp,vbscript,asp. Net,c#,c++,database" >
<meta name= "Description" content= "failing programmers, ubiquitous" >
</HEAD>

<BODY>
<script language= "JavaScript" >
<!--

function 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, fight Emperor Challenge: \ n" a group of battle 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 vs. 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+ "\ n";
Alert (msg);

-->
</SCRIPT>
</BODY>
</HTML>



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.