javascript : 逻辑或条件的检测结果
javascript : Detecting result of a logical OR condition
是否有内置方法来检测逻辑或条件中的哪一半为真?
// foo would return true or false
if (thing1 === "3" || thing2 === foo()) {
// do something if thing1 is 3
// do something different if thing2 is true
};
或者我应该只使用两个嵌套的 if 块吗?
我不确定 "nested" 部分,但通常会简单地完成:
if (thing1 === "3") {
// do something if thing1 is 3
} else if (thing2 === foo()) {
// do something different if thing2 is true
}
使用两个 if 块。没有 built-in 方法可以做到这一点。
此外,如果 foo()
returns 为真或假,您不需要 thing2 === foo()
并且末尾的分号是无关紧要的。
if (thing1 === "3") {
// do something
} else if (foo()) {
// do something else
}
是的。不一起泡吧:
if (thing1 === "3") {
// do something if thing1 is 3
}
else if(thing2 === foo()) {
// do something different if thing2 is 'equal to return value of foo()'
}
嵌套无论如何都行不通,因为它是或条件。
使用 if
和 elseif
是一个不错的选择
if ("3" === thing1) {
// do something if thing1 is 3
} else if (thing2 === foo()) {
// do something different if thing2 is true
}
是否有内置方法来检测逻辑或条件中的哪一半为真?
// foo would return true or false
if (thing1 === "3" || thing2 === foo()) {
// do something if thing1 is 3
// do something different if thing2 is true
};
或者我应该只使用两个嵌套的 if 块吗?
我不确定 "nested" 部分,但通常会简单地完成:
if (thing1 === "3") {
// do something if thing1 is 3
} else if (thing2 === foo()) {
// do something different if thing2 is true
}
使用两个 if 块。没有 built-in 方法可以做到这一点。
此外,如果 foo()
returns 为真或假,您不需要 thing2 === foo()
并且末尾的分号是无关紧要的。
if (thing1 === "3") {
// do something
} else if (foo()) {
// do something else
}
是的。不一起泡吧:
if (thing1 === "3") {
// do something if thing1 is 3
}
else if(thing2 === foo()) {
// do something different if thing2 is 'equal to return value of foo()'
}
嵌套无论如何都行不通,因为它是或条件。
使用 if
和 elseif
是一个不错的选择
if ("3" === thing1) {
// do something if thing1 is 3
} else if (thing2 === foo()) {
// do something different if thing2 is true
}