【发布时间】:2020-05-25 23:27:27
【问题描述】:
是否有 javascript es6 或 es-next 的语法来实现以下行为
let t = {
text: 'hello world',
[shouldShow ? ...{'show' : 'test text'} : ''],
test: 21
}
我希望如果 shouldShow 为真对象 t 应该是
{
text: 'hello world',
show: 'test text',
test: 21
}
如果 shouldShow 为 false,则对象 t 将是
{
text: 'hello world',
test: 21
}
我知道,我可以用 if/else 做到这一点,但我想问是否有一种语法可以只用于一行代码
【问题讨论】:
标签: ecmascript-next ecmascript-2016