clones
should deep clone everything even global objects, functions, circularities, ...
Companion for safer-eval.
Runs on node and in modern browsers:
Versions | |
---|---|
node | |
Chrome | 55, 56, 71 |
Firefox | 45, 51, 64 |
Edge | 14, 16 |
IE | |
Safari | 10 |
iOS Safari | 10 |
Installation
npm i -S clones
Usage
const clones = const dest =
Parameters
Parameters
source: Object
, clone source
bind: Object
, bind functions to this context
Returns: Any
, deep clone of source
Example:
const clones = var source = obj: a: b: 1 arr: true 1 c: 'dee' { return thisnumber + 12 }// adding circularitysourceobjae = sourceobja // do the cloning (with binding a context)var dest = // => { obj: { a: { b: 1, e: [Circular] }, d: 2017-02-17T21:57:44.576Z },// arr: [ true, 1, { c: 'dee' } ],// fn: [Function: fn] } // checksassert // has different referenceassert // has different referenceassert // has different referenceassert // different references for circularitiesassert // has same contentassert // has different function referencesourcefn = sourcefn // bind `this` for `source`assert // returning the same result
Clone prototypes or classes
const clones = // clone built in `Array`const C = clonesclassesArray let c = 123// => [1, 2, 3]c// => [3, 2, 1]