Javascript Javascript在if语句中返回false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3711377/
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
Javascript return false in if statements
提问by designerdre101
Is it good practice to use "return false;" to basically say do nothing in an if statement? For example:
使用“return false”是个好习惯吗?基本上说在if语句中什么都不做?例如:
if (navigator.userAgent.match(/iPad/i) != null) {
return false;
} else {
//Usual script here
}
just wondering if there are any downfalls to this. I can use the if statement without the else but i'm just wanting to get insight on this. I have a plugin that i do not want running on iPad and so I'm wrapping it in the conditional. any comments would be appreciated!
只是想知道这是否有任何缺点。我可以在没有 else 的情况下使用 if 语句,但我只是想对此有所了解。我有一个不想在 iPad 上运行的插件,所以我将它包装在条件中。任何意见将不胜感激!
回答by epascarello
Group 1 will say it is horrible practice since it is hard to follow.
第 1 组会说这是可怕的做法,因为很难遵循。
Group 2 will say do it.
第 2 组会说去做。
Group 3 will say do it, but in 1 line
第 3 组会说这样做,但在 1 行中
Group 4 will say do not use the else
第 4 组会说不要使用 else
Group 5 will say to do not use the return, just use the if around the code you want to run. AKA:
第 5 组会说不要使用 return,只需在要运行的代码周围使用 if。又名:
if (navigator.userAgent.match(/iPad/i) === null) {
//Usual script here
}
回答by MRR0GERS
In my experience only if you were actually looking for the false to be returned.
根据我的经验,只有当您真正在寻找要返回的假货时。
回答by Nick Craver
You can actually simplify it further, like this:
您实际上可以进一步简化它,如下所示:
if (navigator.userAgent.match(/iPad/i) != null) return false;
//Usual script here
Is it "good practice"...sure, if it works for you and your team. Jumping out of a function as soon as you have nothing more to do there is a very efficient way to do things, as long as it's easy to understand and maintain for whoever is working on it.
这是“好的做法”吗……当然,如果它对您和您的团队有效。只要您无事可做,就立即跳出一个函数,这是一种非常有效的做事方式,只要它易于理解和维护,供从事该功能的人使用。
Whether you want falsespecifically depends on the situation, for example if you want to return but not prevent other event handlers from running later, you may want return true;instead.
您是否需要false具体取决于情况,例如,如果您想返回但不阻止其他事件处理程序稍后运行,您可能需要return true;。
回答by RobertPitt
firstly it is very good practise, take this example
首先这是一个很好的实践,以这个例子为例
var window.__page_loaded__;
var Loadpage = function ()
{
if(window.__page_loaded__ != undefined)
{
return; //The page has already laoded
}
//Proceed to load the page
}
by using the return;you doing the same as you would with an elsestatement but without the extra block, and as Loadpage()would normally not return any data its perfectly fine to short cut your code.
通过使用return;您,您可以像使用else语句一样但没有额外的块,并且Loadpage()通常不会返回任何数据,因此可以完美地缩短您的代码。
回答by Vivin Paliath
You should only return a value if a caller is going to do something with that value. If you want to do "nothing" in an if statement, it's a sign that your logic is wrong. Change your statement to this:
如果调用者打算使用该值执行某些操作,您应该只返回一个值。如果您想在 if 语句中“什么都不做”,则表明您的逻辑是错误的。将您的声明更改为:
if (navigator.userAgent.match(/iPad/i) == null) {
//Usual script here
}
This way you don't need to "break" out of your function with the return (not a good practice in this scenario).
这样你就不需要在返回时“打破”你的函数(在这种情况下不是一个好习惯)。
回答by Máthé Endre-Botond
I agree with snkmchnb, otherwise just negate the condition. You can negate long expression using these:
我同意snkmchnb,否则就否定条件。您可以使用这些否定长表达式:
!(a && b) = !a || !b
!(a || b) = !a && !b
And use these several times to get what you want. For example, negating a long expression
并多次使用这些以获得您想要的。例如,否定一个长表达式
!( (a && b || c) && (d || e) || f) =
!((a && b || c) && (d || e)) && !f =
(!(a && b || c) || !(d || e)) && !f =
(!(a && b) && !c || !d && !e) && !f =
((!a || !b) && !c || !d && !e) && !f
This looks ugly now, but negating most times doesn't mean over-complicating. For example negating "<=" results in ">"
这现在看起来很难看,但大多数时候否定并不意味着过于复杂。例如否定“<=”导致“>”
So never use !(long_expression) neither:
所以永远不要使用 !(long_expression) :
if (long expression)
{
}
else
{
//do stuff here
}

