1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- var news = {
- name: '赵家锐',
- age: 29,
- msg: {
- address: '哈尔滨',
- sex: '男'
- }
- }
- function deepClone(obj) {
- // 定义一个空对象 用于接收深拷贝后的结果
- var obj1 = {};
- for(var key in obj) {
- // 判断对象中的每一项是不是引用数据类型
- if(typeof(obj[key]) == 'object'){
- // 是引用类型
- obj1[key] = deepClone(obj[key]);
- } else {
- // 不是引用类型
- obj1[key] = obj[key];
- }
- }
- return obj1;
- }
- var new1 = deepClone(news);
- new1.msg.address = '北京';
- console.log(new1,'new1')
- console.log(news,'news')
- // console.log(deepClone(news))
- </script>
- </body>
- </html>
|