简体   繁体   English

在尝试使用箭头函数返回三元运算符时,如何修复ES6 Eslint错误?

[英]How can I fix ES6 Eslint errors while trying to return a ternary operator in arrow function?

I understand that () => {} does not need return, however if it is not there then Eslint complains about unused expressions. 我知道() => {}不需要返回,但如果不存在则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
}

Here is the function: it tries to find out whether some specified date( to ) minus specified period( target ) is later in time than from . 下面是函数:它试图找出一些指定日期(是否to )减去指定时间内( target )后是在时间上比from It should return true if so and false in the opposite case. 如果是这样,它应该返回true而在相反的情况下应该返回false。 I keep bumping into eslint error expected assignment to a function call and instead saw expression . 我不断碰到eslint错误, expected assignment to a function call and instead saw expression

I tried to rewrite it several times, but in most iterations I got `arrow function expects no return error, ex.: 我尝试多次重写它,但在大多数迭代中我得到`箭头函数期望没有返回错误,例如:

return (toUnixTimestamp(to) - target > toUnixTimestamp(from)) ? return(toUnixTimestamp(to) - target> toUnixTimestamp(from))? true : false 真假

I understand that () => {} does not need return 我明白()=> {}不需要返回

That's not the case. 事实并非如此。 Arrow functions only implicitly return when what followed the => is a single expression . =>后面的内容是单个表达式时,箭头函数仅隐式返回。 If you use => { , the opening bracket { indicates the start of a function block, and you do indeed need to explicitly return at the end of the block (or wherever else you want to return something). 如果你使用=> { ,开括号{表示一个功能块的开头,你确实需要在块的末尾显式return (或者你想要return其他地方)。

At the moment, your code isn't returning anything at all - that's what the linting error is trying to tell you - the true : false is going unused at the moment, it's just an orphaned expression. 目前,你的代码根本没有返回任何东西 - 这就是linting错误试图告诉你的 - true : false现在没有被使用,它只是一个孤立的表达式。

So, just add the return statement to the beginning of your conditional: 因此,只需将return语句添加到条件的开头:

export const isInInterval = (from, to, target) => {
  if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
    return
  }
  const toUnixTimestamp = time => new Date(time).getTime()
  return toUnixTimestamp(to) - target > toUnixTimestamp(from)
    ? true
    : false
}

Or, because > evaluates to a boolean already, you might omit the conditional operator entirely: 或者,因为>已经评估为boolean ,您可以完全省略条件运算符:

return toUnixTimestamp(to) - target > toUnixTimestamp(from)

Here's an example of how you would write an arrow function that does use implicit return: 这里是你如何写一个箭头函数, 使用隐式返回的例子:

export const isEarlyTimestamp = (timestamp) => (
  timestamp < 4000000
  ? true
  : false
);

try this: 试试这个:

export const isInInterval = (from, to, target) => {
    if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
    return false
    }
    const toUnixTimestamp = time => new Date(time).getTime()
    return toUnixTimestamp(to) - target > toUnixTimestamp(from);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM