function deepClone(obj) {
if(obj === null || typeof obj !=='object') return obj
if(obj instanceof Date) return new Date(obj)
if(obj instanceof RegExp) return new RegExp(obj)
let newObj = new obj.constructor()
for (let key in obj) {
if(obj.hasOwnProperty(key)) {
newObj[key] = deepClone(obj[key])
}
}
return newObj
}
案例:
let a = {
name : '张三',
age : 26,
reg: /^.${1,25}$/,
like : {
id: 123,
index : [3,4,5,6]
}
}
let b = deepClone(a)
console.log(a==b) // 输出false