* 深拷贝对象
(obj: T)
| 68 | * 深拷贝对象 |
| 69 | */ |
| 70 | public static deepClone<T>(obj: T): T { |
| 71 | if (obj === null || typeof obj !== 'object') { |
| 72 | return obj; |
| 73 | } |
| 74 | |
| 75 | if (obj instanceof Date) { |
| 76 | return new Date(obj.getTime()) as T; |
| 77 | } |
| 78 | |
| 79 | if (Array.isArray(obj)) { |
| 80 | return obj.map((item) => this.deepClone(item)) as T; |
| 81 | } |
| 82 | |
| 83 | if (obj instanceof Map) { |
| 84 | const cloned = new Map(); |
| 85 | for (const [key, value] of obj.entries()) { |
| 86 | cloned.set(key, this.deepClone(value)); |
| 87 | } |
| 88 | return cloned as T; |
| 89 | } |
| 90 | |
| 91 | if (obj instanceof Set) { |
| 92 | const cloned = new Set(); |
| 93 | for (const value of obj.values()) { |
| 94 | cloned.add(this.deepClone(value)); |
| 95 | } |
| 96 | return cloned as T; |
| 97 | } |
| 98 | |
| 99 | // 普通对象 |
| 100 | const cloned = {} as Record<string, unknown>; |
| 101 | for (const key in obj) { |
| 102 | if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 103 | cloned[key] = this.deepClone((obj as Record<string, unknown>)[key]); |
| 104 | } |
| 105 | } |
| 106 | return cloned as T; |
| 107 | } |
| 108 | } |