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

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

我想这样写:
hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}},

最佳答案

传播is not an operator,它是对象文字语法的一部分(或者至少是在接受建议时)。你需要写

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

关于javascript - 使用语句传播语法ES6,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45184666/

10-09 22:44