Skip to content

Function: clone()

ts
function clone<T>(obj): T;

Creates a shallow clone of the given value. 创建给定值的浅克隆。

Only the top-level structure is copied; nested objects and arrays retain the same references as the original.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
objTThe value to shallow clone / 要浅克隆的值

Returns

T

A shallow clone of the value / 值的浅克隆

Example

typescript
const obj = { a: 1, b: { c: 2 } }
const cloned = clone(obj)
cloned.a = 99
obj.a // => 1 (top-level copy, original unchanged)
cloned.a // => 99
cloned.b.c = 99
obj.b.c // => 99 (nested object is the same reference)

Since

1.0.0

Released under the MIT License.