评论

收藏

[jQuery] jQuery深拷贝

开发技术 开发技术 发布于:2021-07-29 21:08 | 阅读数:271 | 评论:0

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery深拷贝</title>
</head>
 
<body> 
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
  <script>
  $(function () {
    var myAction = {};
    $.extend(myAction, {
      initPage: function (pointA, pointB) {
        var myTest = {
          a: 'a',
          b: 'b'
        };        
        var myTest2 = jQuery.extend({}, myTest);
        myTest2.b = 'b2';
        console.log(myTest.b === 'b2');  //false
        var myArr = [1, 2];        
        var myArr2 = jQuery.extend([], myArr);
        myArr2[1] = 3;
        console.log(myArr[1] === 3);  //false    
        var myArrObj = [{a: 1}, {a: 2}];        
        var myArrObj2 = jQuery.extend([], myArrObj);
        myArrObj2[1].a = 3;
        console.log(myArrObj[1].a === 3);  //true 注意:未能实现深拷贝          
        var myArrObj = [{a: 1}, {a: 2}];        
        var myArrObj2 = jQuery.extend(true, [], myArrObj);
        myArrObj2[1].a = 3;
        console.log(myArrObj[1].a === 3);  //false               
      }
    });
    var init = function () {
      myAction.initPage();
    }();
  });
  </script>
  <script>
  </script>  
</body>
</html>