淺度克隆
function clone(origin,target){
var target = target || {};
for(var prop in origin){
target[prop]=origin[prop];
}
return target;
}
深度克隆
function deepClone(origin,target){
var target = target || {};
var toStr = Object.prototype.toString;
var arrStr = "[object Array]";
for(var prop in origin){
if(origin.hasOwnProperty(prop)){
if(origin[prop]!="null" && typeof(origin[prop])=='object'){
if(toStr.call(origin[prop])==arrStr){
target[prop]=[];
}else{
target[prop]={};
}
deepClone(origin[prop],target[prop]);
}else{
target[prop]=origin[prop];
}
}
}
return target;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者