javascript - 使用语句扩展语法 ES6

标签 javascript ecmascript-next spread-syntax

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

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/

相关文章:

javascript - 如何使用 Typescript 通过类型而不是接口(interface)来建模对象?

javascript - 在 c3.js 中使用 JSON 数组作为类别轴

javascript - fat-arrow 函数返回的剩余运算符

javascript - 在 React 组件中,foo(){} 和 bar = () => {} 有什么区别,什么时候应该使用哪个?

javascript - 传播语法返回意外的对象

javascript - 有没有办法在按键时应用 mask ? (JQuery)

javascript - ng-repeat 在对象上部分循环

javascript - 异步/等待隐式返回 promise ?

javascript - Babel 加载器错误 : rest/spread operator not understood

javascript - 从属性对象创建对象的更好方法?