javascript - es6 使用常量解构对象

标签 javascript ecmascript-6

我正在使用 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/

相关文章:

javascript - 防止默认添加 deps.js

javascript - meteor 自动成型 : form with id "asdf" needs either "schema" or "collection" attribute

javascript - 在 NextJS 中获取客户端和服务器端的数据

Javascript "for of"在 IE 11 中失败

用于深度优先搜索的 Javascript 扩展运算符

javascript - 为什么达到目标后就倒计时了?

javascript - JavaScript(或 jQuery)中是否有 'has focus'?

JavaScript:任何人都可以让它工作或解释为什么它不起作用吗?

ecmascript-6 - 嵌套es6数组解构?

javascript - React.js,ES6,如何访问ES6类中的react-router Lifecycle?