对象的浅拷贝与深拷贝
2018-07-02 本文已影响0人
铜牛彦祖
浅拷贝
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;
}