Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clone offers foolproof deep cloning of variables in JavaScript.

Installation

npm install clone

or

npm install -g clone

Example

var 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.

API

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

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!

About

deep cloning in JavaScript

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages