- Source:
Recursively clone an object.
const {Deepclone, deepclone} = require('ferrum');
class Bar {
constructor(foo, bang) {
this.foo = foo;
this.bang = bang;
}
[Deepclone.sym]() {
return new Bar(deepclone(this.foo), deepclone(this.bar));
}
}
const a = new Bar({foo: 42}, {bar: 5});
const b = deepclone(a);
assert(a !== b);
assertEquals(a, b);
a.foo.foo = 5;
assert(b.foo.foo === 42);
Interface
(x: TheValue) => r: TheValue
Laws
x !== rx equals rwehre eq is the equals() function.get(r, k) !== get(x, k)for any k.has(r, k) implies has(x, k)for any k.get(r, k) equals get(x, k)for any k wehre eq is the equals() function.- The above laws apply recursively for any children.
Specialization Notes
No implementation provided for set: In sets keys and values are the
same thing.
If we cloned sets deeply, has(orig, key) implies has(clone, key) would be violated
and the sets would not be equal after cloning.
For the same reason, Map keys are not cloned either!
See: deepclone