本文介绍了Square Brackets 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"

这篇关于Square Brackets Javascript对象密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:22