我正在使用 es6 解构功能,并且执行了以下操作
const state = { a: 1 };
const ID = 'b';
const newState = { ...state, [ID]: 2 }
如您所见,我使用常量的值作为标识符,生成这个新对象。
//newStateValue ==> { a:1, b:2 }
我想知道 [ID] 是如何工作的?
谢谢
最佳答案
它的名字是Computed Property Names它与 deconstructing 无关.
property names. That allows you to put an expression in brackets [], that will be computed and used as the property name. This is reminiscent of the bracket notation of the property accessor syntax, which you might have used to read and set properties already. Now you can use a similar syntax in object literals too
关于javascript - es6 使用常量解构对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48009168/