Javascript开关case:使用单个方法计算case语句

Javascript开关case:使用单个方法计算case语句,javascript,switch-statement,Javascript,Switch Statement,是否可以使用接受单个开关值并返回布尔值的函数来测试单个case语句 如果没有,除了使用If语句外,还有没有更有效的(速度或可读性)方法来运行类似的测试 下面的示例用于说明 在下面的代码中: IsInt(value)是一个函数,如果值是整数,则返回true或false isMultipleOfThree(值)是一个函数,如果值是三的倍数,则返回true或false var switchVal = "9"; switch( switchVal ){ case(isMultipleOfThree

是否可以使用接受单个开关值并返回布尔值的函数来测试单个case语句

如果没有,除了使用If语句外,还有没有更有效的(速度或可读性)方法来运行类似的测试

下面的示例用于说明

在下面的代码中: IsInt(value)是一个函数,如果值是整数,则返回true或false isMultipleOfThree(值)是一个函数,如果值是三的倍数,则返回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.');
 }

一个
if或者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
ismultipleof theree
返回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;
 }

不,但是您可以使用
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.`);
  }
);

无论
case()
部分中的函数是什么,此语法都不会按照您的预期工作。如果你的第一个
案例没有中断,那么如果第一个案例是真的,你的第二个案例总是会被击中。示例:我使用嵌套的三元语句在onceThanks@Santi上实现了这一点-作为一个示例,我将其固定为一致的。@craignewkirk还使用了C方法和标志为我的答案添加了另一个替代方法。是的,这正是我试图避免的,因为我进行了几次测试。我知道我可以用if语句来实现这一点,但用switch语句看起来更可读。Plus switch语句通常在规模上具有性能优势。@craignewkirk您是对的。我错误地认为你所有的开关都有
中断。感谢您提供的附加解决方案,Loaf!我也考虑过使用位标志,但正在寻找更具可读性的东西。最终,在切换测试中使用“true”是我的情况下最具可读性的解决方案。正如我前面提到的,如果值是整数,但不是三的倍数,那么示例中的控制台仍将打印“7是整数。7是三的倍数。”示例:@Santi所以它应该执行所有正确的情况?我的意思是我认为很明显,传递一个像7这样的数字不应该记录“7是三的倍数”…如果你忘记了一个中断,那么脚本将从满足条件的情况开始运行,然后不管是否满足条件都将运行该情况。这就是为什么它说7是3的倍数。您的代码在第一个案例之后没有
中断
,因此它失败了。我试图理解如果通过
9
,您希望它做什么。