对象文字中属性名称周围的方括号是什么意思? [英] What do square brackets around a property name in an object literal mean?

查看:16
本文介绍了对象文字中属性名称周围的方括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 JS 写了一段时间,没有用过这种形式:

I've been writing in JS for a while and have not used this form:

  dist: {
    files: {
      [bpr + 'lib/Monster.min.js']: ['<%= concat.dist.dest %>']
    }
  }
}

[]:[]

它有效,只是我之前没有使用过或见过它.

it works, I just have not used it or seen it before.

推荐答案

最近才使用 ES6.他们被称为计算的属性名"

Only recently with ES6. They are called "computed property names"

来自 MDN:

从 ECMAScript 2015 开始,对象初始值设定项的语法也支持计算属性名称.这允许你放置一个表达式在方括号 [] 中,这将被计算为属性名称.

Starting with ECMAScript 2015, the object initializer syntax also supports computed property names. That allows you to put an expression in brackets [], that will be computed as the property name.

这篇关于对象文字中属性名称周围的方括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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