Object.prototype.toString.call(obj) Javascript中,一切皆为对象
Object.prototype.toString.call(2) // “[object Number]”
Object.prototype.toString.call(‘’) // “[object String]”
Object.prototype.toString.call(true) // “[object Boolean]”
Object.prototype.toString.call(undefined) // “[object Undefined]”
Object.prototype.toString.call(null) // “[object Null]”
Object.prototype.toString.call(Math) // “[object Math]”
Object.prototype.toString.call({}) // “[object Object]”
Object.prototype.toString.call([]) // “[object Array]”
function deepClone(obj,map= new Map()){
if(typeof obj !== 'object' || obj == null) return obj
if(map.has(obj)) return map.get(obj)
let newObj = Object.prototype.toString.call(obj) == '[object Array]'?[]:{}
map.set(obj,newObj)
for(let key in obj){
if(typeof obj[key] === 'object' && obj[key] !== null && obj.hasOwnProperty(key)){
newObj[key] = deepClone(obj[key],map)
}else{
newObj[key] = obj[key]
}
}
}