Suppose I have a javascript class:
class SomeClass {
constructor() {
this.field1 = [];
this.field2 = 0;
this.field3 = "somestring";
}
someMethod(param1) {
// do some magical stuff
}
}
At some point I have a need to merge two objects, one of them is of SomeClass type.
const obj1 = new SomeClass();
const obj2 = {
field1: [1, 2, 3],
field2: [3, 4, 5],
field3: "other string"
}
const mergedObj = {...obj1, ...obj2};
Now I have lost the someMethod method in mergedObj variable, throwing a TypeError:
mergedObj.someMethod("amazingparam"); //=> Uncaught TypeError: mergedObj.someMethod is not a function
How can I keep the method definitions?
Edit
To be more clear: I would like to create a new object, keep the obj1 and obj2 as they were.