【发布时间】:2019-01-22 00:47:49
【问题描述】:
我知道() => {} 不需要返回,但是如果它不存在,那么Eslint 会抱怨未使用的表达式。
export const isInInterval = (from, to, target) => {
if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
return
}
const toUnixTimestamp = time => new Date(time).getTime()
toUnixTimestamp(to) - target > toUnixTimestamp(from) ? true : false
}
这里是函数:它试图找出某个指定的日期(to)减去指定的时间段(target)是否比from晚。如果是这样,它应该返回 true,在相反的情况下返回 false。
我一直遇到 eslint 错误expected assignment to a function call and instead saw expression。
我尝试重写了几次,但在大多数迭代中我得到了`箭头函数期望没有返回错误,例如:
return (toUnixTimestamp(to) - 目标 > toUnixTimestamp(from)) ?真:假
【问题讨论】:
-
很难看出发生了什么,因为你的代码太紧凑了!我会添加更多中间变量以更好地了解正在发生的事情。短而紧凑的一行代码不会提高代码的可读性。简单明了的代码提高了可读性。
-
根据@givanse 的评论,您可以考虑像
isDateValid这样的函数,然后您的第一行可以是if (!isDateValid(to) || !isDateValid(from)) return;
标签: javascript ecmascript-6 eslint ternary-operator