【发布时间】:2019-04-10 09:41:23
【问题描述】:
当涉及到 if 语句时,可以重构此代码
(这只是一个例子,并不指“真实”的代码)
if(person === 'customer' || person === 'employee' || person === 'other')
到
if(person === ('customer' || 'employee' || 'other'))
目前我有一个名为 state 的对象,其中包含 3 个布尔属性。如果至少一个属性返回true,我想显示一个叠加层。我目前的解决方案是这样的
showOverlay: state => state.isNavigating || state.isHttpRequesting || state.isProcessing
我在问是否有办法清理它。伪代码将是
showOverlay: state => (isNavigating || isHttpRequesting || isProcessing) of state
我知道它没有什么大收获,但它会删除所有 state. ... 部分。
【问题讨论】:
-
it is possible to refactor this code不,不是,它们是两个不同的结果 -
['customer', 'employee', 'other'].includes(person)但这与您的其他代码完全不同 -
@CertainPerformance - OP 声明第二段代码是对第一段的有效重构!
-
state => { with(state) { return isNavigating || isHttpRequesting || isProcessing; } }... 开个玩笑 :) -
很抱歉,
premature optimization is the root of all evil是我目前最大的敌人 .. :(
标签: javascript