switch (buttonValue) { case '8': // ... return case specialCase(buttonValue): // having trouble getting here}const specialCase = (buttonValue) => buttonValue > '4' && buttonValue !== '8'如何specialCase在 switch 语句中输入块?buttonValue = 7例如,如果specialCase()函数返回true,但我从不输入那个 case 块。我需要将逻辑抽象到单独的specialCase()函数中,因为我正在检查的某些条件很复杂。基本上,我试图避免看起来像这样的 case 语句:case buttonValue > '4' && buttonValue !== '8' && buttonValue < '19' && .... etc我只是错误地格式化了 case 语法,还是您不能使用函数的返回值来输入 case?
1 回答

明月笑刀无情
TA贡献1828条经验 获得超4个赞
中的表达没有错case
。只需将您的功能移到switch
.
另外,正如评论中提到的那样 - switch 不是true
/false
的东西。为了case
匹配 ,您需要在函数中返回 case 值,而不仅仅是true
.
添加回答
举报
0/150
提交
取消