【发布时间】:2018-09-04 12:36:48
【问题描述】:
condition ?
domElement.classList.add('show') :
domElement.classList.remove('show');
上面的代码有效,但是 DOM 变量和 classList 被显式键入了两次。有没有办法使用三元组只将链的不同部分放在它们各自的真/假子句中?
我在想这样的事情:
domElement.classList condition ? .add('show') : .remove('show');
非常感谢任何和所有输入。
【问题讨论】:
-
domElement.classList[condition ? 'add' : 'remove']('show');
标签: javascript ternary-operator code-duplication method-chaining