在if语句中获取失败条件
get failing condition in if statement
考虑下面的if语句
if (a === null || b === null || c === null) {
// I want the failing condition
}
是否有可能得到失败的条件而不需要检查每一个
if (a === null || b === null || c === null) {
if (a===null){alert('a failed the check');}
if (b===null){alert('b failed the check');}
if (c===null){alert('c failed the check');}
}
我知道在上面的例子中,很容易使它动态,考虑一个真实世界的例子,其中执行不同的测试。
不可能,在if
块中不可能得到求值为true的条件。
当然,因为您使用了or
条件,您的代码可以简单地为
if (a===null){alert('a failed the check');}
else if (b===null){alert('b failed the check');}
else if (c===null){alert('c failed the check');}
不带外部if
。
如果你想知道哪个条件是失败的,那么你需要明确地声明,在你的if
条件,否则有没有方式。像这样:
if(a===null){alert('a failed the check');}
else if (b===null){alert('b failed the check');}
else {alert('c failed the check');}
边注:
当您使用||
运算符时,一旦满足第一个false
条件,它就不会检查下一个条件。
你可以做类似的事情:
var failed = false;
if (a===null){alert('a failed the check');failed=true;}
if (b===null){alert('b failed the check');failed=true;}
if (c===null){alert('c failed the check');failed=true;}
if (failed) { /* common logic */ }
相关文章:
- JS中的条件运算符-失败的语法错误
- JavaScript 条件总是失败
- 量角器自定义预期条件失败并显示错误
- 谷歌数据存储查询中的多项选择抛出 API 错误:前提条件节点中失败错误
- 如果条件失败,则正在提交表单
- 未能加载资源:服务器的响应状态为412(前提条件失败)
- 在社交网络上获得412(前提条件失败),原因
- 当条件失败时,如何取消选中特定复选框
- jQuery AJAX $.Ajax和$.对成功或失败条件都不发出警报
- if语句条件检查!= "undefined"失败
- Javascript if else条件运行时失败
- POST方法即使if条件失败也能工作
- 在if语句中获取失败条件
- 如果条件失败,尝试返回默认字符串
- Javascript切换按钮在if条件下失败
- 如果条件失败
- 量角器-当元素不可见且没有执行'else"时测试失败条件
- 使用条件填充失败
- jquery隐藏/显示或条件失败
- AccessDenied根据策略无效:策略条件失败:[“eq”,“$success_action_redirect”;h