打赏

es6 对象浅拷贝的2种方法

<!DOCTYPE html>
<html lang="zh">

    <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>es6 对象浅拷贝</title>
    </head>

    <body>
        <script type="text/javascript">
            var obj = {
                'data': [11, 2, 3],
                'name': 'mfg',
                fn: function() {}
            };
            var objNew = { ...obj
            };
            var objNew2 = Object.assign({}, obj);
            console.log(objNew === obj) //false
            console.log(objNew2 === obj) //false
            console.log(objNew.fn === obj.fn) //true
            console.log(objNew2.fn === obj.fn) //true
        </script>
    </body>

</html>

 

posted @ 2018-05-07 19:39  孟繁贵  阅读(1113)  评论(0编辑  收藏  举报
TOP