Function: deepClone()
ts
function deepClone<T>(obj): T;Creates a deep clone of the given value, handling circular references. 创建给定值的深度克隆,处理循环引用。
This function recursively copies all properties of objects and arrays, including nested objects, while maintaining the original data types for dates, regular expressions, and other built-in objects. 此函数递归复制对象和数组的所有属性,包括嵌套对象, 同时保持日期、正则表达式和其他内置对象的原始数据类型。
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the value to clone / 要克隆的值的类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | T | The value to deep clone / 要深度克隆的值 |
Returns
T
A deep copy of the input value / 输入值的深度副本
Example
typescript
const original = { name: 'John', hobbies: ['reading', 'gaming'], address: { city: 'New York' } }
const cloned = deepClone(original)
console.log(cloned.hobbies.push('swimming')) // 3 — push returns new length
original.hobbies // => ['reading', 'gaming'] — original unchanged
// Handles circular references
const circular = { name: 'test' }
circular.self = circular
const clonedCircular = deepClone(circular) // works without infinite recursionSince
1.0.0