1

I wonder that is there easy way to set this properties with constructor parameters like using spread operator or another way?

this is clasical way of setting props of this:

constructor(id=0, name='', surname='') {
  this.Id = id;
  this.Name = name;
  this.Surname = surname;
}

I am looking for another way to set this props in one line :)

uzay95
  • 16,052
  • 31
  • 116
  • 182

1 Answers1

5

You can use Object#assign to merge an object created from the params to this:

constructor(id=0, name='', surname='') {
  Object.assign(this, { id, name, surname });
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209