最近没什么学习计划 自己动手写下简单的面试题
function deepClone(data) {
//判断是否为空
if (!data) return data;
//处理特殊数据类型
let type = [Date, RegExp, Set, Map, WeakMap, WeakSet];
if(type.includes(data.constructor)) {
return new data.constructor(data);
}
let dataClone = Array.isArray(data) ? [] : {};
if (typeof (data) === "object") {
for (key in data) {
if (data.hasOwnProperty(key)) {
if (data[key] && typeof (data[key]) === "object") {
dataClone[key] = deepClone(data[key])
} else {
dataClone[key] = data[key]
}
}
}
//如果不是数组和对象就返回
} else {
return data;
}
return dataClone
}