英文:
Have a switch statement check multiple cases without fall-through
问题
以下是您要翻译的内容:
有没有办法使开关语句检查并可能分别执行每个案例?
switch (true) {
case (condition1): // this could be true or not
// Do thing 1...
case (condition2): // this could be true or not
// Do thing 2...
default: // do this if all others are false
// Do thing 3...
break;
}
我希望能够执行thing 1,thing 2,两者或两者都不执行(默认执行thing 3)。
在每个情况下使用break不起作用,因为它无法执行thing 1和thing 2。贯穿不起作用,因为即使只有一个为真,它也会执行所有情况。
开关语句是否有办法实现这一点?是否有更好的方法来做到这一点?
英文:
Is there a way to have a switch statement check and possibly execute each case individually?
switch (true) {
case (condition1): // this could be true or not
// Do thing 1...
case (condition2): // this could be true or not
// Do thing 2...
default: // do this if all others are false
// Do thing 3...
break;
}
I want to be able to do thing 1, thing 2, both, or neither (default to thing 3).
Using break in each case doesn't work because it can't do thing 1 and thing 2. Fall-through doesn't work because it does all of them even if only one is true.
Is there a way for switch statements to do this? Is there a better way to do this?
答案1
得分: 1
以下是您要翻译的内容:
You could take a series of if
statements and call things.
A not recommended use of switch
. Just add a break in the last clause of the group.
If you like to have a more dynamic approach, you could add a case
clause with a value not equal to the switch
value, like false
, NaN
or Symbol()
.
case new Symbol:
break
The last two expressions are not equal to any other (not stored) value.
英文:
You could take a series of if
statements and call things.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
f = (condition1, condition2) => {
if (condition1) console.log('thing 1');
if (condition1 || condition2) console.log('thing 2');
else console.log('thing 3');
console.log('---');
};
f(false, false); // thing3
f(false, true ); // thing2
f(true, false); // thing1 thing 2
f(true, true ); // thing1 thing 2
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
A not recommended use of switch
. Just add a break in the last clause of the group.
If you like to have a more dynamic approach, you could add a case
clause with a value not equal to the switch
value, like false
, NaN
or Symbol()
.
case new Symbol:
break
The last two expressions are not equal to any other (not stored) value.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
f = (condition1, condition2) => {
switch (true) {
case condition1:
console.log('thing 1');
case condition2:
console.log('thing 2');
break; // stops falling into default clause
default:
console.log('thing 3');
}
console.log('---');
};
f(false, false); // thing3
f(false, true ); // thing2
f(true, false); // thing1 thing 2
f(true, true ); // thing1 thing 2
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论