cloneDeep 深克隆实现
Favori,
图:Nguyen Nhut
测试对象
var obj = {
//原数据,包含字符串、对象、函数、数组等不同的类型
name: "test",
main: {
a: 1,
b: 2,
},
fn: function () {
alert(1);
},
date: new Date(),
reg: new RegExp(),
friends: [1, 2, 3, [22, 33]],
};
浅克隆
function clone(obj) {
let newObj = {};
for (let i in obj) {
newObj[i] = obj[i];
}
return newObj;
}
深克隆
function cloneDeep(obj) {
let newObj = null;
if (typeof obj === "object" && typeof obj !== null) {
if (obj instanceof Array) {
newObj = [];
for (let i in obj) {
newObj[i] = cloneDeep(obj[i]);
}
} else if (obj instanceof Date) {
newObj = new Date(obj)
} else if (obj instanceof RegExp) {
newObj = new RegExp(obj)
} else {
newObj = {};
for (let i in obj) {
newObj[i] = cloneDeep(obj[i]);
}
}
} else {
newObj = obj;
}
return newObj;
}