JavaScript
clone()

clone()

The clone() method clones the passed value and returns it with a new reference.

  • If the value is an array or an object, a deep clone is performed on its elements recursively.
  • If the value is a date, a new date object with the same time value is returned.

Usage

import { clone } from '@ponsetya/core'

Syntax

clone(
  value: any,
  options?: {
    transfer?: Transferable[]
  }
): any

Parameters

value: any
The value to clone.
value: object
Optional options to control the cloning process.
transfer: Transferable[]

An optional list of Transferable objects that should be included in the cloning process.

Returns: any
The cloned value with a new reference.

Examples

const obj1 = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
  },
  hobbies: ['reading', 'gardening'],
}
 
const obj2 = clone(value)
 
console.log(obj1 === obj2) // false