是否可以使用接受单个开关值的函数并返回布尔值来测试单个case语句?
如果没有,除了使用if语句之外,是否有更高效(速度或可读性)的方式来运行类似的测试?
下面举例说明。
在下面的代码中: IsInt(value)是一个函数,如果值是整数,则返回true或false isMultipleOfThree(value)是一个函数,如果值是三的倍数,则返回true或false。
var switchVal = "9";
switch( switchVal ){
case(isMultipleOfThree):
console.log(switchVal + ' is a multiple of three.');
case(isInt):
console.log(switchVal + ' is an integer.');
break;
default:
console.log(switchVal + ' is not an integer or a multiple of three.');
}
答案 0 :(得分:1)
if else if
怎么样?
var switchVal = "9";
if(isInt(switchVal)){
console.log(switchVal + ' is an integer.');
}else if(isMultipleOfThree(switchVal)){
console.log(switchVal + ' is a multiple of three.');
}else{
console.log(switchVal + ' is not an integer or a multiple of three.');
}
然而,这并没有解决问题,如果它是9,它将退出并且只打印9是整数。您可以将它们分成不同的块。
var switchVal = "9";
if(isInt(switchVal)){
console.log(switchVal + ' is an integer.');
if(isMultipleOfThree(switchVal)){
console.log(switchVal + ' is a multiple of three.');
if(!isInt(switchVal) && !isMultipleOfThree(switchVal))
console.log(switchVal + ' is not an integer or a multiple of three.');
}
但是当你添加越来越多的测试时,这可能会变得混乱。这当然假设您有两个函数isInt
和isMultipleOfThree
返回true或false。
采用带有标志的C方法的另一种奇怪的选择。
function isInt(s){
if (!isNaN(s))
return 1; //0001
return 0;
}
function isMultipleOfThree(s){
var int = parseInt(s);
if(int % 3 === 0)
return 2; //0010
return 0;
}
function isSevem(s){
var int = parseInt(s);
if(int === 7)
return 4; // 0100
return 0;
}
var switchVal = "7";
console.log(7 % 3 === 0);
var result = isSevem(switchVal) + isMultipleOfThree(switchVal) + isInt(switchVal);
switch( result ){
case 1: // 0001
console.log(switchVal + ' is an integer.');
break;
case 2: // 0010
console.log(switchVal + ' is a multiple of three.');
break;
case 3: // 0011
console.log(switchVal + ' is an integer and a multiple of three.');
break;
case 4: // 0100
console.log(switchVale + ' is the number seven.');
break;
case 5: // 0101
console.log(switchVal + ' is an integer and is the number seven.');
break;
case 6: // 0110
console.log(switchVal + ' is a multiple of three and is the number seven.');
break;
case 7: // 0111
console.log(switchVal + ' is an integer and is a multiple of three and is the number seven.');
break;
default:
console.log(switchVal + ' is not an integer or a multiple of three.');
break;
}
答案 1 :(得分:1)
不,但你可以使用switch(true) {}
做类似的事情:
var switchVal = "9";
switch( true ){
case isInt(switchVal):
console.log(switchVal + ' is an integer.');
case isMultipleOfThree(switchVal):
console.log(switchVal + ' is a multiple of three.');
break;
default:
console.log(switchVal + ' is not an integer or a multiple of three.');
}
如果您尝试执行所有传递案例,您可以编写一个执行此操作的函数:
function last(arr) {
return arr[arr.length - 1];
}
function allCases(value, ...cases) {
let defaultCase;
let hasPassedAny = false;
if(typeof last(cases) === "function") {
defaultCase = last(cases);
cases = cases.slice(0, -1);
}
cases.forEach(([predicate, resultFn]) => {
if(predicate(value)) {
hasPassedAny = true;
resultFn(value);
}
});
if(defaultCase && !hasPassedAny) {
defaultCase(value);
}
}
allCases(switchVal,
[isInt, (v) => {
console.log(`${v} is an integer`);
}],
[isMultipleOfThree, (v) => {
console.log(`${v} is a multiple of three`);
}],
(v) => {
console.log(`${v} is not an integer or a multiple of three.`);
}
);