实现一个函数clone
likunNet opened this issue · comments
实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制
let newobj
let str
if (typeof obj !== 'object') {
return
} else if (window.JSON) {
str = JSON.stringify(obj)
newobj = JSON.parse(str)
} else {
for (const i in obj) {
if ({}.hasOwnProperty.call(obj, i)) {
newobj[i] = typeof obj[i] === 'object' ?
this.clone(obj[i]) : obj[i]
}
}
}
return newobj