是否有内置方法可以检测逻辑或条件中的哪一半为真?
// 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 block ?
最佳答案
使用两个 if block 。没有内置方法可以执行此操作。
此外,如果 foo()
返回 true 或 false,则不需要 thing2 === foo()
并且末尾的分号是无关的。
if (thing1 === "3") {
// do something
} else if (foo()) {
// do something else
}
关于JavaScript : Detecting result of a logical OR condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562956/