I'm trying to merge the props from values
into this
.
The following throws an error. How can I do this?
this = {...this, ...values}
You could extend this
with Object.assign method:
class Foo {
constructor (props) {
Object.assign(this, props);
}
}
const foo = new Foo({ a: 1 });
console.log(foo.a); // 1
Seeing your use of the spread operator, it looks like you're using a Stage 3 proposal for ECMAScript.
https://github.com/tc39/proposal-object-rest-spread
Try the Object.assign method:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Object.assign(target, ...sources);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With