javascript “返回”后不必要的“其他”。(无其他返回)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46875442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unnecessary 'else' after 'return'. (No-else-return)
提问by David Brierton
I am using es-lint to clean up the errors in my code. I have come across this error:
我正在使用 es-lint 来清理代码中的错误。我遇到了这个错误:
Unnecessary 'else' after 'return'. (No-else-return)
“返回”后不必要的“其他”。(无其他返回)
} else {
I have always used else statements after a return. Is there something I may be overlooking?
我总是在返回后使用 else 语句。有什么我可能会忽略的吗?
if (cctot <= 3 && cctot > 0) {
alert('Credit under .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;
回答by codejockie
What that is basically saying is that the else part of the if statement is unnecessary if there is a returnin the ifpart.
Something like this is what it expects:
那是什么基本上说的是,if语句的else部分是不必要的,如果有一个return在if部分。像这样的事情是它所期望的:
if (cctot <= 3 && cctot > 0) {
alert('Credit under .00 not allowed');
return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
}
cctot *= -1;
In general, this:
一般来说,这:
if (condition) {
return something;
} else {
// do another thing
}
return anotherThing;
is similar to:
类似于:
if (condition) {
return something;
}
// do another thing
return anotherThing;
After the ifwith a returnstatement, there is no need for the elsepart as the code below the ifwill only run when the condition stated is not fulfilled.
在ifwithreturn语句之后,不需要该else部分,因为下面的代码if只会在不满足声明的条件时运行。
回答by llama
It's a code style preference. You don't need the elseand instead can put the elsecode directly below the if. This is because if the ifsucceeds, that's the end of the function, so the elsecode will never be reached anyway.
这是一种代码风格偏好。您不需要else,而是可以将else代码直接放在if. 这是因为如果if成功,那就是函数的结束,所以else无论如何都不会到达代码。
So this:
所以这:
if (condition) {
return foo;
} else {
// do bar
}
return baz
is equivalent to this:
相当于:
if (condition) {
return foo;
}
// do bar
return baz
This style seems to vary in different programming communities. Go developers will nearly always omit the else, while I've seen more JS devs include it.
这种风格在不同的编程社区中似乎有所不同。Go 开发人员几乎总是会省略else,而我已经看到更多的 JS 开发人员包含它。
While I prefer to leave off the else, it is again purely a matter of preference. Don't let it worry you too much. People may get dogmatic about this kind of thing, but it's really not that important.
虽然我更喜欢不使用else,但这又是一个纯粹的偏好问题。不要让它让你太担心。人们对这种事情可能会变得教条化,但它真的没有那么重要。
回答by Puyi Severino
The return statement stops/terminates the current function. It's just saying that there's no need for 'else' since the execution of the function already stopped and if the 'if' condition doesn't succeed, it will still run any code underneath it.
return 语句停止/终止当前函数。这只是说不需要'else',因为函数的执行已经停止,如果'if'条件不成功,它仍然会运行它下面的任何代码。
As for best practice, I won't say it's a big of a deal always but with the code in your example, I won't use the else clause because it's simply not needed. I think it's good to understand what's happening under the hood and the reason behind best practices and not just following them.
至于最佳实践,我不会说它总是很重要,但是对于您示例中的代码,我不会使用 else 子句,因为它根本不需要。我认为最好了解幕后发生的事情以及最佳实践背后的原因,而不仅仅是遵循它们。

