Skip to content

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; // » true

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; // » false

Notation is for data objects with enumerable properties. clone() deeply copies:

  • plain objects and arrays,
  • primitives — String, Number, Boolean, Symbol, null, undefined,
  • built-ins Date and RegExp.

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);