深度拷贝封装函数

深度拷贝封装函数

function DeepCopy(obj) {
  if (typeof obj !== 'object' || obj === null) return obj
  let request
  obj instanceof Array ? request = [] : request = {}
  for (let i in obj) {
    request[i] = DeepCopy(obj[i])
  }
  return request
}
let obj = { name: '李白', age: 1315, work: ['静夜思', '赠汪伦', '将进酒'] }
let copyObj = DeepCopy(obj)
console.log(copyObj);
copyObj.name = '杜甫'
console.log(obj);
console.log(copyObj);

你可能感兴趣的:(js,javascript)