“返回”后不必要的“其他”。 (不返回) [英] Unnecessary 'else' after 'return'. (No-else-return)

查看:1659
本文介绍了“返回”后不必要的“其他”。 (不返回)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用es-lint清理代码中的错误。我遇到了这个错误:

I am using es-lint to clean up the errors in my code. I have come across this error:


返回后不必要的其他。 (不返回)

Unnecessary 'else' after 'return'. (No-else-return)



} else {

我总是在返回后使用else语句。

I have always used else statements after a return. Is there something I may be overlooking?

   if (cctot <= 3 && cctot > 0) {
      alert('Credit under $3.00 not allowed');
      return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
    } else {
      cctot *= -1;
    }
  }
  return precise(cctot);
}
module.exports = calculateCredit;


推荐答案

基本上是说,如果 if 部分中有 return ,则if语句是不必要的。
是这样的期望:

What that is basically saying is that the else part of the if statement is unnecessary if there is a return in the if part. Something like this is what it expects:

if (cctot <= 3 && cctot > 0) {
      alert('Credit under $3.00 not allowed');
      return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
}
cctot *= -1;

通常,这是:

if (condition) {
  return something;
} else {
  // do another thing
}

return anotherThing;

类似于:

if (condition) {
  return something;
}

// do another thing
return anotherThing;

if 后加上 return 语句,不需要 else 部分作为 if 仅在不满足指定条件时运行。

After the if with a return statement, there is no need for the else part as the code below the if will only run when the condition stated is not fulfilled.

这篇关于“返回”后不必要的“其他”。 (不返回)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆