如何使用新的 ES6 功能编写以下内容:
this.currentPlayer = values.currentPlayer;
this.gameOver = values.gameOver;
this.inCheck = values.inCheck;
我相信我应该使用解构运算符或 Object.assign 函数或两者都使用
最佳答案
据我所知,没有办法有效地简化该代码,除非这三个字段是 values
中唯一存在的字段。
如果(且仅当)是这种情况,您可以使用:
Object.assign(this, values);
有一个解构版本,但恕我直言,它不值得使用,因为它几乎不比单独的显式赋值短:
({currentPlayer: this.currentPlayer,
gameOver: this.gameOver,
inCheck: this.inCheck} = values);
关于javascript - 解构或赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576708/