题目描述

深浅拷贝的区别?有哪些实现浅拷贝的方式?有哪些实现深拷贝的方式?可以手写一个深拷贝函数吗?

题目分析

考察对JS的理解

参考答案

深浅拷贝的区别:

在JS中,变量的类型大致分为两种:基本数据类型和引用数据类型,其中基本数据类型包括:undefined,Null,Boolean,Number,String。基本数据类型保存在栈内存中,引用类型保存在堆内存中,而栈内存中保存的必须是大小固定的数据,引用类型的大小不固定只能保存在堆内存中,所以我们访问基本数据类型是直接访问它的值,而引用类型我们是通过它保存在内存中的地址来操作的,从而也就有了深浅拷贝的区别。
深浅拷贝的区别主要体现在:我们通过复制一个对象得到一个变量,如果这个变量会跟随原对象的改变而改变,则是浅拷贝,否则称之为深拷贝。

浅拷贝的实现方法:

方法一: 直接赋值

  1. const personA = {
  2. name: 'AAA',
  3. age: 10
  4. }
  5. const personB = personA
  6. personA.name = 'aaa'
  7. console.log(personB === personA) // true
  8. console.log(personB) // { name: 'aaa', age: 10 }

方法二:Object.assign()

  1. /*
  2. 这个方法看起来很像深拷贝,这个方法是将对象的属性逐一赋值到新对象中,
  3. 如果对象属性是基本数据类型,则原对象上这个属性的改变不会改变新对象上的这个属性,
  4. 但如果对象中的属性是如果是对象的话,这个属性仍然与原对象中的属性指向同一个地址,
  5. 值会因原对象的改变而改变。
  6. */
  7. const personA = {
  8. name: 'AAA',
  9. age: 10,
  10. jobs: {
  11. company: 'AC',
  12. years: 2
  13. }
  14. }
  15. const personB = Object.assign({},personA)
  16. console.log(personA === personB) // false
  17. personA.name = 'aaa'
  18. personA.jobs.company = 'ac'
  19. console.log(personB) // { name: 'AAA', age: 10, jobs: { company: 'ac', years: 2 } }
  20. //可以看到name不会改变,而jobs中的属性却改变了,这也印证了我们上述的内容,
  21. //Object.assign()仍然是一个浅拷贝的方法

方法三: 通过ES6中的展开运算符

  1. const personA = {
  2. name: 'AAA',
  3. age: 10,
  4. jobs: {
  5. company: 'AC',
  6. years: 2
  7. }
  8. }
  9. const personB = {...personA}
  10. console.log(personA === personB) // false
  11. personA.name = 'aaa'
  12. personA.jobs.company = 'ac'
  13. console.log(personB) //{ name: 'AAA', age: 10, jobs: { company: 'ac', years: 2 } }
  14. //可以看到展开运算符的效果跟Object.assign()是几乎差不多的

方法四: 通过lodash的_.clone方法

  1. const personA = {
  2. name: 'AAA',
  3. age: 10,
  4. jobs: {
  5. company: 'AC',
  6. years: 2
  7. }
  8. }
  9. const personB = _.clone(personA)
  10. console.log(personA === personB) // false
  11. personA.name = 'aaa'
  12. personA.jobs.company = 'ac'
  13. console.log(personB) //{ name: 'AAA', age: 10, jobs: { company: 'ac', years: 2 } }
  14. //效果与方法二、三一样

方法五: 数组的concat方法

  1. const arr1 = [1,2,3,[4,44]]
  2. const arr2 = arr1.concat()
  3. console.log(arr1 === arr2) //false
  4. arr1[0] = 777
  5. arr1[3][0] = 555
  6. console.log(arr2) // [ 1, 2, 3, [ 555, 44 ] ]

方法六:数组的slice方法

  1. const arr1 = [1,2,3,[4,44]]
  2. const arr2 = arr1.slice(0)
  3. console.log(arr1 === arr2) //false
  4. arr1[0] = 777
  5. arr1[3][0] = 555
  6. console.log(arr2) // [ 1, 2, 3, [ 555, 44 ] ]
  7. //基本与方法五效果一样

深拷贝的实现方法:

方法一:JSON.parse(JSON.stringify(obj))

  1. // 最简单粗暴实现深拷贝的方法,但性能没有那么好,并且只能处理json能解析的数据格式
  2. const personA = {
  3. name: 'AAA',
  4. age: 10,
  5. jobs: {
  6. company: 'AC',
  7. years: 2
  8. }
  9. }
  10. const personB = JSON.parse(JSON.stringify(personA))
  11. console.log(personA === personB) // false
  12. personA.name = 'aaa'
  13. personA.jobs.company = 'ac'
  14. console.log(personB) //{ name: 'AAA', age: 10, jobs: { company: 'AC', years: 2 } }

方法二: 使用loadsh的_.cloneDeep()方法

  1. const personA = {
  2. name: 'AAA',
  3. age: 10,
  4. jobs: {
  5. company: 'AC',
  6. years: 2
  7. }
  8. }
  9. const personB = _.cloneDeep(personA)
  10. console.log(personA === personB) //false
  11. personA.name = 'aaa'
  12. personA.jobs.company = 'ac'
  13. console.log(personB) // { name: 'AAA', age: 10, jobs: { company: 'AC', years: 2 } }

方法三: jQuery的extend()方法

  1. const personA = {
  2. name: 'AAA',
  3. age: 10,
  4. jobs: {
  5. company: 'AC',
  6. years: 2
  7. }
  8. }
  9. const personB = $.extend(true,{},personA)
  10. console.log(personA === personB) //false
  11. personA.name = 'aaa'
  12. personA.jobs.company = 'ac'
  13. console.log(personB) //{ name: 'AAA', age: 10, jobs: { company: 'AC', years: 2 } }

手写一个深拷贝函数

  1. function deepCopy(obj){
  2. let result;
  3. // 获取obj的类型,主要分为三类,1.Array 2.Object 3.普通数据类型
  4. const type = Object.prototype.toString.call(obj)
  5. // 根据类型进行递归
  6. if(type === '[object Array]'){
  7. result = []
  8. obj.forEach((item,index)=>{
  9. result[index] = deepCopy(item)
  10. })
  11. }else if(type === '[object Object]'){
  12. result = {}
  13. for(let key in obj){
  14. result[key] = deepCopy(obj[key])
  15. }
  16. }else return obj // 如果是普通数据类型直接返回即可,因为普通数据类型是按值访问的
  17. return result
  18. }
  19. const personA = {
  20. name: 'AAA',
  21. age: 10,
  22. jobs: {
  23. company: 'AC',
  24. years: 2
  25. }
  26. }
  27. const personB = deepCopy(personA)
  28. console.log(personA === personB) // false
  29. personA.name = 'aaa'
  30. personA.jobs.company = 'ac'
  31. console.log(personB) //{ name: 'AAA', age: 10, jobs: { company: 'AC', years: 2 } }