Javascript 检查开关是否有外壳

Javascript 检查开关是否有外壳,javascript,switch-statement,Javascript,Switch Statement,这在我正在工作的项目中出现过几次,我如何“测试”一个开关来确定它是否有一个案例,而不实际执行它 如果必须运行该案例,是否有有效的检查方法 先谢谢你 i、 e 我所知道的确定开关是否有您的case的唯一方法是实际运行case语句。如果想知道案例块是否实际运行,可以返回布尔值 function runSwitch(_case) { var hasCase = false; switch(_case) { case 'casename0': hasCase = true


这在我正在工作的项目中出现过几次,我如何“测试”一个开关来确定它是否有一个案例,而不实际执行它

如果必须运行该案例,是否有有效的检查方法

先谢谢你

i、 e


我所知道的确定开关是否有您的case的唯一方法是实际运行case语句。如果想知道案例块是否实际运行,可以返回布尔值

function runSwitch(_case) {
    var hasCase = false;
    switch(_case) {
        case 'casename0': hasCase = true; alert('case'); break;
        case 'casename1': hasCase = true; alert('case'); break;
        case 'casename2': hasCase = true; alert('case'); break;
        case 'casename3': hasCase = true; alert('case'); break;
        default: break;
    }
    return hasCase;
}
然后,可以使用以下命令运行该语句:

if(runSwitch('casename4')) {
    //The case statement ran
}
else {
    //The case statement did not run
}

不管你将要检查每一个案例,所以通过开关运行它是最优的。如果您只想在运行之前检查案例是否存在,请将它们添加到数组中并检查索引是否存在

var cases = ['case1', 'case2'];
if (cases.IndexOf('case1') >= 0) {
    // the case is there
}

由于David Schwartz没有给出答案,我将发布我的解决方案(这几乎不是一个解决方案)以及我所理解的他的解决方案的演示和解释

我的解决方案: 我只是停止使用开关,转而使用JSON(数组),因为开关的唯一用途是根据输入设置变量

使用数组时,检查“case”是否存在很容易(只需运行
arrayVar[“casename”]
,如果它不存在,则返回未定义),并且不需要用额外的变量阻塞名称空间,运行代码稍微有点困难,因为它需要作为字符串和
eval
ed提供,但总的来说,这对我来说效果更好

没有必要发布演示或代码,因为它实际上不是解决此问题的方法

David Schwartz的解决方案: 演示:

代码:

function hasCase(caseName) { return switchName(caseName, true); } // A function to check for a case, this passes the case name to switchName, sets "just checking" to true and passes the response along
function runSwitch(caseName) { switchName(caseName); } // This is actually a mostly useless function but it helps clarify things

function switchName(caseName, c) { // And the actual function, c is the "just checking" variable, I shortened it's name to save code
    switch(caseName) { // The switch
        case "casename0" : if (c) return true; alert("case"); break; // If c is true return true otherwise continue with the case, returning from inside a case is the same as calling break;
        case "casename1" : if (c) return true; alert("case"); break;
        case "casename2" : if (c) return true; alert("case"); break;
        case "casename3" : if (c) return true; alert("case"); break;
        default: if (c) return false; break; // If nothing else ran then the case doesn't exist, return false
    }
    return c; // Even I (:D) don't understand where this gets changed, but it works
}
说明:上面的代码根据需要进行注释。

我不明白你的问题。您提供给runSwitch'switch.hasCase'的参数表明switch是一个带有hasCase方法的Javascript对象。这是我正在寻找的一个示例,它可能存在(可能不存在)如果是这样的话,我想不出更好的方法来引用它。你可以用一个包含函数的对象替换switch语句来替换你的case,但是听起来好像你做错了什么-你首先要做的是什么?你的解决方案非常好,只是我必须复制每个case名称,这实际上是我目前的解决方案,我正在寻找一种方法来避免这种情况。创建一个函数,可以运行开关,也可以只检查一个案例。在每个案例之后,输入类似于
if(justChecking)的内容返回true
default
情况下,如果(justChecking)返回false,则输入
if。然后为单个函数创建两个包装函数,
runSwitch
hasCase
,它们使用不同的值
justChecking
调用单个函数。我想切换到JSON数组,因为我不是真正执行代码,只是设置变量值。。。如果你给我一个答复,我会接受的。
function hasCase(caseName) { return switchName(caseName, true); } // A function to check for a case, this passes the case name to switchName, sets "just checking" to true and passes the response along
function runSwitch(caseName) { switchName(caseName); } // This is actually a mostly useless function but it helps clarify things

function switchName(caseName, c) { // And the actual function, c is the "just checking" variable, I shortened it's name to save code
    switch(caseName) { // The switch
        case "casename0" : if (c) return true; alert("case"); break; // If c is true return true otherwise continue with the case, returning from inside a case is the same as calling break;
        case "casename1" : if (c) return true; alert("case"); break;
        case "casename2" : if (c) return true; alert("case"); break;
        case "casename3" : if (c) return true; alert("case"); break;
        default: if (c) return false; break; // If nothing else ran then the case doesn't exist, return false
    }
    return c; // Even I (:D) don't understand where this gets changed, but it works
}