Skip to content

clone

Creates a shallow copy of the given object/value.

198 bytes
since v12.1.0

Usage

Creates a shallow copy of the given object or array.

Notably, built-in objects like Map, Set, Date, and others are not supported.

import * as
import _
_
from 'radashi'
const
const ra: {
name: string;
power: number;
}
ra
= {
name: string
name
: 'Ra',
power: number
power
: 100,
}
const
const gods: {
name: string;
power: number;
}[]
gods
= [
const ra: {
name: string;
power: number;
}
ra
]
import _
_
.
function clone<{
name: string;
power: number;
}>(obj: {
name: string;
power: number;
}): {
name: string;
power: number;
}

Creates a shallow copy of the given object/value.

@see ― https://radashi.js.org/reference/object/clone

@example

const original = { a: 1, b: { c: 3 } }
const cloned = clone(original)
// => { a: 1, b: { c: 3 } }
original !== cloned
// => true
original.b === cloned.b
// => true

@version ― 12.1.0

clone
(
const ra: {
name: string;
power: number;
}
ra
) // => copy of ra
import _
_
.
function clone<{
name: string;
power: number;
}[]>(obj: {
name: string;
power: number;
}[]): {
name: string;
power: number;
}[]

Creates a shallow copy of the given object/value.

@see ― https://radashi.js.org/reference/object/clone

@example

const original = { a: 1, b: { c: 3 } }
const cloned = clone(original)
// => { a: 1, b: { c: 3 } }
original !== cloned
// => true
original.b === cloned.b
// => true

@version ― 12.1.0

clone
(
const gods: {
name: string;
power: number;
}[]
gods
) // => copy of gods

Differences from spread syntax

The clone function is similar to spread syntax (e.g. {...obj}), but it also preserves the prototype of the original object. If a constructor property exists on the original prototype, it will be called with the new keyword to create a new instance, and all properties will then be copied over to it.

Unlike spread syntax, clone also supports cloning arrays.