javascript - 对象属性名称的箭头函数的简写
问题描述
当我有:
const foo = 1;
我可以为对象属性名称设置简写
const bar = {foo}
那是一样的
const bar = {foo: foo} // bar will be {foo: 1}
当我有一个带箭头函数的 const 时,有速记吗?
const foo = () => 1
我必须这样设置,但它不酷
const bar = {foo: foo()}
我想做这样或者更短的东西
const bar = {foo()} //bar should be {foo: 1} but this will fail
解决方案
Looking at the specs, there's no syntax for this.
In your case ObjectLiteral
must resolve to { PropertDefinitionList }
, which must resolve to PropertyDefinition
. Now what could PropertyDefinition
be?
It can't be IdentifierReference
- that's your first example: {f}
.
CoverInitializedName
doesn't really fit either. That would look like {f = AssignmentExpression}
where AssignmentExpression
is defined here.
PropertyName: AssignmentExpression
is the conventional syntax: {f: 1}
MethodDefinition
lets you do {f(){ return 1; }};
, but that again isn't what you want.
Looks like the verdict is no.
推荐阅读
- docker - Hyperledger - Docker swarm 在部署到多个主机时失败
- php - 将 php 脚本添加到网站登录/注册功能时出现 HTTP ERROR 500
- mysql - 删除重复的用户元
- python - 使用 python 生成的网页作为 Pygame 控件的输入
- docker - Kubernetes - 如何使用 pod 中目录中的文件?
- infiniband - Infiniband 传输层
- javascript -
- shell - 在 bash 中对文本文件进行排序
- html - 不以“r”结尾的类属性
- laravel - 使用 MaterializeCSS 进行分页