我有一些(我认为它们可能很愚蠢)问题......但它们只是问题,我想在这里发帖并进行辩论。
这是关于条件的。我正在使用 javascript 进行开发,我有一些功能,我想编写最佳代码。
例如,我必须检查一些条件,所以我可以写:
if(text == ""){
//some code for finish
}else if(text== previousText){
//some code for finish
}
//here I write more code...which runs if both conditions had not complied.
我的疑问是:你认为该怎么做更好?
第 1 点
if(text == ""){
//some code for finish
}else if(text== previousText){
//some code for finish
}else{
//here I write more code...which runs if both conditions had not complied.
}
或
第2点
if(text == ""){
//some code for finish using return
return;
}else if(text== previousText){
//some code for finish using return
return;
}
//here I write more code...which runs if both conditions had not complied.
我希望已经解释得很好。有时这些事情会被忽视,但我认为它们很重要。 非常感谢,丹尼尔
最佳答案
我遵循的规则是每个函数只有一个 return
语句。所以第1点
关于javascript - 关于 javascript 中的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835639/