使用语句扩展语法ES6 [英] Spread syntax ES6 with statement

查看:114
本文介绍了使用语句扩展语法ES6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用扩展语法编写三元运算符并复制两个对象.是否可以在文字对象内部使用具有扩展语法的三元运算符?我的代码可以正常工作,我只想对其进行优化.

I tried to write ternary operator with spread syntax and copy two objects. Is it possible to use ternary operator with spread syntax inside with literal objects? My code works okay, I just want to optimize it.

hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint,

我想这样写:

hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}},

推荐答案

传播 不是运算符,它是对象文字语法的一部分(或者至少在接受建议时才会存在).你需要写

Spread is not an operator, it's part of the object literal syntax (or at least it will be when the proposal is accepted). You need to write

{...globalStyles.hint, ...(disabled ? globalStyles.hintDisabled : {})},

这篇关于使用语句扩展语法ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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