Tutorials to .com

Tutorials to .com » Jsp » Skills » Javascript in the shallow and deep copy copies of the realization of

Javascript in the shallow and deep copy copies of the realization of

Print View , by: iSee ,Total views: 19 ,Word Count: 756 ,Date: Sat, 22 Aug 2009 Time: 10:06 AM

javascript in the assignment and the like is the same as in Java, for reference.
In other words, in a like assignment to a variable, then the variable is still the point is like the original address. That the answer is how to do this cloning.

What is a "clone"?

In the actual programming, we often encounter this situation: It is an object A, in a moment of A already contains a number of RMS, at this time may need a new object and A is identical to B, and then of B would not be affected by any changes to the value of A, that is to say, A and B are two independent, but the initial value of B is determined by the A objects. In java / javasript languages, with a simple assignment statements can not meet this demand. To meet this demand although there are a lot of ways, but the realization of clone () method is one of the simplest and most efficient means, of course, the javascript language does not this method.

Therefore, I deliberately wrote the two cloning methods: a shallow copy, a deep copy.
Shallow copy (shadow clone): just like to copy the basic types, like type, is still in the original quote.
Deep copy (deep clone): copy does not bear the basic categories of objects, but also copy the original object like. That is entirely generated by the new object.

The following is a code, I already tested, and have not had any problems, hope that we are interested to participate.
<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.0 Transitional / / EN">
<TITLE> New Document </ TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META NAME="Generator" CONTENT="Wawa Editor 1.0">
<META NAME="Keywords" CONTENT="javascript,Java,XML,XSLT,ASP,VBScript,ASP .net,C#,C++,Database">
<META NAME="Description" CONTENT=",">
</ HEAD>

<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 = "First";
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 The King of Fighters on their own profession Cup Challenge The King of Fighters: \ n \ n A group against the form of: \ n \ n"
msg + = obj1.Team + "group list:" + obj1.Powers + "\ n";
msg + = "after the first round, the winner for:" + obj1.winner.name + ", No age:" + obj1.winner.age + ", from the island:" + obj1.winner.from + "\ n ";
msg + = "\ n \ n B team against the form of: \ n \ n"
msg + = obj1_clone.Team + "group list:" + obj1_clone.Powers + "\ n";
msg + = "after the first round, the winner for:" + obj1_clone.winner.name + ", No age:" + obj1_clone.winner.age + ", from the international police force:" + obj1_clone.winner.from + " \ n ";
alert (msg);

</ BODY>
</ HTML>

Java skills and code Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.