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>

猜你喜欢

转载自www.cnblogs.com/mengfangui/p/9004595.html