原生js实现深浅拷贝

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chi1130/article/details/82084177

 浅拷贝:

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    // 浅拷贝
    let obj = {a:{b:10}}
    function copy(obj){
      let newObj = {};
      for(let attr in obj){
        newObj[attr] = obj[attr]
      }
      return newObj
    }
    let obj2 = copy(obj);
      obj2.a.b = 20;
      console.log(obj.a.b);  // 20 改变
  </script>
</body>
</html>

 深拷贝:

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
      // 深拷贝
      function deepCopy(obj) { 
        if(typeof obj != 'object'){
          return obj;
        }
        let newObj = {};
        for(let attr in obj){
          newObj[attr] = deepCopy(obj[attr])
        }
        return newObj
       }

       let obj2 = deepCopy(obj);
        obj2.a.b = 20;
       console.log(obj.a.b);  // 10 未改变
  </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/chi1130/article/details/82084177