clone-shallow
v0.1.1
Published
Shallow clone objects and arrays, or return primitive values directly.
Downloads
1,227
Maintainers
Readme
clone-shallow
Shallow clone objects and arrays, or return primitive values directly.
Install
Install with npm:
npm i clone-shallow --save-dev
Usage
clone(value);
value
{*} The value to clone.return
{*} The cloned value.
Example
Arrays:
var a = ['a', 'b', 'c'];
var b = clone(a);
a = a.concat('new');
console.log('a:', a);
//=> a: [ 'a', 'b', 'c', 'new' ]
console.log('b:', b);
//=> b: [ 'a', 'b', 'c' ]
Objects:
var extend = require('xtend');
var a = {foo: 'alpha', bar: 'beta', baz: 'gamma'};
var b = cloneObject(a);
a = extend(a, {quux: 'delta'});
console.log('a:', a);
//=> a: { foo: 'alpha', bar: 'beta', baz: 'gamma', quux: 'delta' }
console.log('b:', b);
//=> b: { foo: 'alpha', bar: 'beta', baz: 'gamma' }
Also see:
- clone-array if you just need to clone arrays.
- clone-object if you just need to clone objects.
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on June 30, 2014.