【发布时间】:2018-02-26 15:13:33
【问题描述】:
基本上这就是我想要完成的。
class Person {
constructor (obj) {
this.first = ''
this.last = ''
this.age = ''
if (obj) {
Object.assign(this, ...obj)
}
}
}
const a = new Person()
console.log('Not spreading: ', a)
const b = new Person({ first: 'Alex', last: 'Cory', age: 27 })
console.log('Spreading: ', b)
有没有办法传播这样的对象来填充类?
【问题讨论】:
标签: javascript class spread-syntax