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>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!