Can't assign this in constructor
This question already has an answer here:
你可以用Object.assign方法扩展this
:
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);
链接地址: http://www.djcxy.com/p/27334.html上一篇: 两个对象的Javascript联合
下一篇: 不能在构造函数中赋值