On This Page
Cloning Utilities
The Cloning utilities provide functions for creating copies of objects and arrays in JavaScript. These functions offer ways to create shallow or deep copies of data structures, which is crucial for avoiding unintended side effects when working with complex objects.
Functions
clone
Creates a deep clone of the provided value.
Handling Complex Structures This function can clone arrays, objects, dates, regular expressions, maps, and sets. It also handles circular references to prevent infinite recursion.
Parameters
Name | Type | Description |
---|---|---|
src | any | The value to clone |
seen | Map | Internal use. A Map to keep track of circular references |
Returns
A deep clone of the input value.
Example
extend
Extends an object with properties from other objects, properly handling getter/setters.
Descriptor Handling This function uses
Object.getOwnPropertyDescriptor
andObject.defineProperty
to ensure that getters and setters are correctly copied, preserving their behavior in the extended object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The target object to extend |
sources | …object | One or more source objects |
Returns
The extended object.