jQuery深拷贝

 

<!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>

 

 

posted @ 2018-08-27 17:55  徐同保  阅读(1839)  评论(0编辑  收藏  举报