0

How can I write the following using new ES6 features:

    this.currentPlayer = values.currentPlayer;
    this.gameOver = values.gameOver;
    this.inCheck = values.inCheck;

I believe I should either use destructuring operator or Object.assign function or both

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

2

AFAIK there's no way to usefully simplify that code, unless those three fields are the only ones present in values.

If (and only if) that is the case, you can just use:

Object.assign(this, values);

There is a destructuring version, but IMHO it's not worth using because it's barely any shorter than individual explicit assignments:

({currentPlayer: this.currentPlayer,
  gameOver: this.gameOver,
  inCheck: this.inCheck} = values);
Alnitak
  • 334,560
  • 70
  • 407
  • 495