【问题标题】:Destructuring or assign operator [duplicate]解构或分配运算符[重复]
【发布时间】:2016-06-05 05:46:59
【问题描述】:
如何使用新的 ES6 功能编写以下内容:
this.currentPlayer = values.currentPlayer;
this.gameOver = values.gameOver;
this.inCheck = values.inCheck;
我认为我应该使用解构运算符或 Object.assign 函数或两者都使用
【问题讨论】:
标签:
javascript
ecmascript-6
【解决方案1】:
AFAIK 没有办法有效地简化该代码,除非这三个字段是 values 中唯一存在的字段。
如果(且仅当)是这种情况,您可以使用:
Object.assign(this, values);
有一个解构版本,但恕我直言,它不值得使用,因为它几乎不比单独的显式分配短:
({currentPlayer: this.currentPlayer,
gameOver: this.gameOver,
inCheck: this.inCheck} = values);