Javascript在if语句中返回false
使用return false是一种好习惯吗?基本上说if语句什么都不做?例如:
Is it good practice to use "return false;" to basically say do nothing in an if statement? For example:
if (navigator.userAgent.match(/iPad/i) != null) {
return false;
} else {
//Usual script here
}
只是想知道这是否有任何挫折。我可以在没有else的情况下使用if语句,但我只想深入了解这一点。我有一个插件,我不想在iPad上运行,所以我把它包装在有条件的。任何意见将不胜感激!
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!
第1组会说它很难实践,因为很难遵循。
Group 1 will say it is horrible practice since it is hard to follow.
第2组会说这样做。
第3组会说这样做,但是在1行
Group 3 will say do it, but in 1 line
第4组会说不要使用其他
Group 4 will say do not use the else
组5会说不要使用返回,只需使用if周围的您想要运行的代码。
又名:
Group 5 will say to do not use the return, just use the if around the code you want to run. AKA:
if (navigator.userAgent.match(/iPad/i) === null) {
//Usual script here
}