1var obj = {
2
3 name: "test",
4 main: {
5 a: 1,
6 b: 2,
7 },
8 fn: function () {
9 alert(1);
10 },
11 date: new Date(),
12 reg: new RegExp(),
13 friends: [1, 2, 3, [22, 33]],
14};
1function cloneDeep(obj) {
2 let newObj = null;
3 if (typeof obj === "object" && typeof obj !== null) {
4 if (obj instanceof Array) {
5 newObj = [];
6 for (let i in obj) {
7 newObj[i] = cloneDeep(obj[i]);
8 }
9 } else if (obj instanceof Date) {
10 newObj = new Date(obj)
11 } else if (obj instanceof RegExp) {
12 newObj = new RegExp(obj)
13 } else {
14 newObj = {};
15 for (let i in obj) {
16 newObj[i] = cloneDeep(obj[i]);
17 }
18 }
19 } else {
20 newObj = obj;
21 }
22 return newObj;
23}