You can make this a switch
, but it's unclear why you would want to. On first glance, this isn't the kind of situation (selecting amongst a set of values and doing something different for each of them) that you use switch
for.
Here's how, though I don't recommend it:
switch (false) {
case !error1(num, a_id):
case !error2(num, b_id):
case !error3(num, c_id):
case !error4(num, d_id):
// Do nothing
break;
default:
a.innerHTML = num;
break;
}
This works in JavaScript, but not in most other languages that have switch
. The reason it works is that the case
labels are evaluated when the execution point reaches them, and they're guaranteed to be evaluated in source code order. So you're switching on the value false
, which will first be tested (using strict equality, ===
) against the return value of !error1(num, a_id)
, and then if that doesn't match, against !error2(num, a_id)
, etc.; if none of them matches, then they all evaluated true
, and the code in the default
block runs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…