function deepClone(obj) {
//先判断是否是一个对象
if(typeof obj !=="object") return
let newArr = obj instanceof Array ? [] :{}
//循环对象属性
for(let key in obj){
//自身属性
if(obj.hasOwnProperty(key)){
//对象属性不存在新对象中
if(!(key in newArr)){
//日期类型
if(obj[key] instanceof Date){
newArr[key] = new Date(obj[key].getTime())
}
//正则类型
else if(obj[key] instanceof RegExp){
newArr [key] = new RegExp(obj[key])
}
//dom树类型 obj[key].nodeType ===1
else if(typeof obj[key] === 'object' && obj[key] instanceof HTMLDocument){
let elm = document.getElementsByTagName(key)[0]
newArr[key] = elm.cloneNode(elm)
}else{
newArr[key] = typeof obj[key] === 'object' ? deepClone(obj[key]):obj[key]
}
}
}
}
return newArr
}
// deepClone 函数测试效果
const objA = {
name: 'jack',
birthday: new Date(),
pattern: /jack/g,
body: document.body,
others: [123,'coding', new Date(), /abc/gim,]
};
const objB = deepClone(objA);
console.log(objA === objB); // 打印 false
console.log(objA, objB);
console