// 深拷贝简易方式:
function deepClone(obj) {
if (typeof obj === 'object') {
var newObj = obj.constructor === Array ? [] : {}
for (var i in obj) {
newObj[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]
}
} else {
var newObj = obj
}
return newObj
}
// 深拷贝hash方式:
function isObject(o) {
return Object.prototype.toString.call(o) === "[object Object]" ||
Object.prototype.toString.call(o) === "[objectArray]"
}
function deepClone4(o, hash = new Map()) {
if (!isObject(o)) return o //检测是否为对象或者数组
if (hash.has(o)) return hash.get(o)
let obj = Array.isArray(o) ? [] : {}
hash.set(o, obj)
console.log('hhhh',hash)
for (let i in o) {
if (isObject(o[i])) {
obj[i] = deepClone4(o[i], hash)
} else {
obj[i] = o[i]
}
}
return obj
}
var a = {};
a.a = a;
var bb = deepClone4(a)
console.log(bb)
console