Mangler.clone() - DarthJDG/Mangler.js GitHub Wiki
Creates a deep copy of an object or array.
Mangler.clone(object)| Parameter | Type | Default | Description |
|---|---|---|---|
| object | Cloneable | Object instance to clone. |
Returns a deep copy of object.
The Mangler.clone() function attempts to create a deep copy of anything that's passed as the object parameter. The returned object will be identical to the one that's passed, but no references will point to the original data.
data = { firstname: 'John', lastname: 'Smith' };
clone = Mangler.clone(data);
clone.firstname = 'Fred';
/*
data = { firstname: 'John', lastname: 'Smith' }
clone = { firstname: 'Fred', lastname: 'Smith' }
*/By default, Mangler.js can only clone simple arrays, object literals, mangler objects and Date objects. If it encounters an object instance of a type that it doesn't recognise, its reference will be passed to the clone as is, pointing to the original object.
To add cloning support for all native JavaScript objects and typed arrays, see the Mangler-natives module.
To teach Mangler.js how to clone other objects, you can register handler functions for other object types. See Mangler.registerType() for more details.