I want to use Object.assign
to clone an instance of a class, including any methods along with it. Is it possible to do with just Object.assign
or should I be using something like lodash deepClone
?
class Foo {
constructor() {
this.a = 1;
this.b = 2;
}
add() {
return this.a + this.b;
}
}
const foo1 = new Foo();
console.log(foo1.add());
console.log(foo1.b);
// ? Where did the add go?
const foo2 = Object.assign({}, foo1, { b: 99 });
console.log(foo2.add());
console.log(foo2.b);