浅拷贝
var a = {
name : '小明'
};
var b = {};
extend(b,a)
function extend(obj1,obj2) {
for(var attr in obj2) {
obj1[attr] = obj2[attr];
}
}
深拷贝
function deepCopy(obj){ //深拷贝
var newObj = {};
if(typeof obj != 'object'){
//console.trace();
return obj;
}
for(var attr in obj){
newObj[attr] = deepCopy(obj[attr]);
}
//console.trace();
//console.log(newObj);
return newObj;
}
网友评论