clone offers foolproof deep cloning of variables in JavaScript.
npm install cloneor
npm install -g clonevar clone = require('clone');
var a, b;
a = { foo: { bar: 'baz' } };
b = clone(a);
a.foo.bar = 'foo';
console.log(b);This will print:
{ foo: { bar: 'baz' } }
clone masters cloning objects, arrays, Date objects, and RegEx objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example.
clone(obj, circular)
Call clone with circular set to false if you are certain that obj
contains no circular references. This will give better performance if needed.
There is no error if undefined or null is passed as obj.
Circular references? Yep!
var a, b;
a = {hello: "world"};
a.myself = a;
b = clone(a);
console.log(b);
This will print:
{hello: "world", myself: [Circular]}
So, b.myself points to b, not a. Neat!