Javascript在if语句中返回false [英] Javascript return false in if statements

查看:185
本文介绍了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
}

这篇关于Javascript在if语句中返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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