//遍历对象 for(var prop in obj)
//1.判断是不是原始值 typeof() object instaceof toString constructor
//2.判断是数组还是对象
//3.建立相应的数组或对象
//递归
1 var obj = {
2 name: 'abc',
3 age: 123,
4 card: ['visa', 'master'],
5 wife: {
6 name: "bcd",
7 son: {
8 name: 'aaa'
9 }
10 }
11 }
12 var obj1 = {
13 name: obj.name,
14 age: 123,
15 card: [obj.card[0], obj.card[1]],
16 wife: {
17 name : obj.wife.name,
18 son: {
19 name : obj.wife.son.name
20 }
21 }
22 }
28 function deepClone(target, origin) {
29 var target = target || {},
30 toStr = Object.prototype.toString,
31 arrStr = '[object Array]';
32 for (var prop in origin) {
33 if (origin.hasOwnProperty(prop)) {
34 if (origin[prop] !== "null" && typeof (origin[prop]) == 'object') {
35 // if (toStr.call(origin[prop]) == arrStr) {
36 // target[prop] = [];
37 // } else {
38 // target[prop] = {};
39 // }
40 target[prop] = ((toStr.call(origin[prop]) == arrStr)?[]:{})
41 deepClone(target[prop], origin[prop])
42 } else {
43 target[prop] = origin[prop]
44 }
45 }
46 }
47 return target;
48 }
49 deepClone(obj1, obj)