javascript中的对像赋值 与 Java中是一样的 ,都为引用传递.
就是说,在把一个对像赋值给一个变量时,那么这个变量所指向的仍就是原来对像的地址.那怎么来做呢 答案是 克隆.
<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 = "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");
新闻热点
疑难解答