Basically here's what I'm trying to accomplish.
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)
Is there a way to spread an object like this to populate a class?
if (obj) { … }
, because Object.assign handles null and undefined. MDN quote: "Note: Object.assign() does not throw on null or undefined sources." – Adur