在 javascript 中,我有一个 if 语句,我想记录默认为 true 的第一个条件。例如,如果 a === true
那么我想 console.log
a (update: 例如,“a”或“b”或“c”,实际字符)。
有什么想法吗?
if(a || b || c){ console.log(this) }
谢谢大家!
最佳答案
最好的办法当然是use a debugger并在分支到 if
语句主体时检查 a
、b
和/或 c
。 :-)
用代码实现,如果你真的想看到“a”、“b”或“c”,没有捷径 更新我在想什么?当然有捷径:
if(a || b || c){
console.log(a ? "a" : b ? "b" : "c");
}
或者如果您还想查看值:
if(a || b || c){
console.log(a ? "a: " + a : b ? "b: " + b : "c: " + c);
}
原始加长版:
if(a || b || c){
if (a) {
console.log("a: " + a);
}
else if (b) {
console.log("b: " + b);
}
else {
console.log("c: " + c);
}
}
或者(这很棘手而且很多,所以不推荐):
if(a || b || c){
switch (false) {
case !a:
console.log("a: " + a);
break;
case !b:
console.log("b: " + b);
break;
default:
console.log("c: " + c);
}
}
...之所以有效,是因为 switch
语句 case
在运行时按顺序求值。
关于javascript - 我怎样才能找到默认为 true 的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789051/