方括号 Javascript 对象键 [英] Square Brackets Javascript Object Key

查看:17
本文介绍了方括号 Javascript 对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释下面在 JavaScript 中分配键的方法为什么/如何工作?

Can anyone explain how the why/how the below method of assigning keys in JavaScript works?

a = "b"
c = {[a]: "d"}

返回:

Object {b: "d"}

推荐答案

这是新的 ES2015(EcmaScript 规范,正式名称为 ES6)计算属性名称语法.这是您在 ES3/5 中知道的 someObject[someKey] 赋值的简写:

It's the new ES2015 (the EcmaScript spec formally known as ES6) computed property name syntax. It's a shorthand for the someObject[someKey] assignment that you know from ES3/5:

var a = "b"
var c = {[a]: "d"}

是语法糖:

var a = "b"
var c = {}
c[a] = "d"

这篇关于方括号 Javascript 对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆