使用扩展运算符实现数组,对象的深拷贝

1.数组深拷贝

 var array = [{name: ‘jack‘}, 2, 3]  
    var newArr = array
    var copyArr = [...array] 
    array.push({
      age: 18
    })
    console.log(array, ‘array‘) //changed
    console.log(newArr, ‘newArr‘) // changed
    console.log(copyArr, ‘copyArr‘) // keep origin array

2.对象深拷贝

    var obj = {
      name: ‘gaby‘
    }
    var newObj = obj
    var copyObj = {...obj}
    obj.gender = ‘female‘
    console.log(obj, ‘obj‘) //changed 
    console.log(newObj, ‘newObj‘) // changed
    console.log(copyObj, ‘copyObj‘)  //keep origin obj

 

使用扩展运算符实现数组,对象的深拷贝

上一篇:强大的编辑器--vim|学习笔记


下一篇:使用python发邮件