Mutation & Cloning
By default, every method that changes data mutates the source object — the
single exception is filter(), which always
returns a fresh object.
const source = { a: 1 };const result = Notation.create(source).set('b', 2).value;
source; // » { a: 1, b: 2 } (mutated)result === source; // » trueOpt Out with clone()
Section titled “Opt Out with clone()”Call clone() before any mutating method to work on a deep copy and leave the
original untouched:
const source = { a: 1 };const result = Notation.create(source).clone().set('b', 2).value;
source; // » { a: 1 } (untouched)result; // » { a: 1, b: 2 }'b' in source; // » falseWhat clone() Supports
Section titled “What clone() Supports”Notation is for data objects with enumerable properties. clone() deeply
copies:
- plain objects and arrays,
- primitives —
String,Number,Boolean,Symbol,null,undefined, - built-ins
DateandRegExp.
For a full structural clone, clone first with a dedicated library and wrap the result:
import _ from 'lodash';Notation.create(_.cloneDeep(source)).set('b', 2);