- Source:
Shallowly clone an object.
const { strictEqual: assertIs, notStrictEqual: assertIsNot } = require('assert');
const { Shallowclone, shallowclone, assertEquals, Equals, eq } = require('ferrum');
class Bar {
constructor(foo, bang) {
this.foo = foo;
this.bang = bang;
}
[Shallowclone.sym]() {
return new Bar(this.foo, this.bang);
}
[Equals.sym](otr) {
return eq(this.foo, otr.foo) && eq(this.bar, otr.bar);
}
}
const a = new Bar({foo: 42}, {bar: 5});
const b = shallowclone(a);
assertEquals(a, b);
assertIsNot(a, b);
a.foo.foo = 5;
assertIs(b.foo.foo, 5);
Interface
(x: TheValue) => r: TheValue
Laws
x !== rget(r, k) === get(x, k)for any k.
Implementation Notes
No-Op implementations are provided for read only primitive types.
See: shallowclone