我可以实现/把一个数组的开关条件

Can I implement/put an array on a switch conditional?

本文关键字:数组 一个 开关 条件 实现 我可以      更新时间:2023-09-26

我正在构建我的代码时,我想到了一个奇怪的想法,我可以实现/把一个数组在一个开关?

我的意思是,我如何使codeHide 情况工作?这段代码不起作用。

当我要求设置命令并将hide()(即codeHide[0]放在codeHide数组上)时,我希望switch采用codeHide的情况(我的if语句)并返回一个alert,告诉我该特定数组元素的alertMessage

如果我把hide(background)(即codeHide[1]codeHide数组上),我想切换到codeHide的情况下(我的If语句),并返回一个alert,告诉我特定数组元素的alertMessage(在is-语句中)。

希望你能理解我。

这样做它不起作用,我认为这是因为"case codeHide:"。

这是我到目前为止所做的:

var codeHide = ['hide()', 'hide(background)'];
$(".code").on("click", function () {
    var codePrompt = prompt("Set the code in the command line."),
    alertMessage = "",
    consoleMessage = "Used '" + codePrompt + "' command.";
switch (codePrompt) {
    case codeHide:
        if (codeHide[0]) {
            alertMessage = "Hiding elements...";
        } else {
            alertMessage = "Hiding Background...";
        }
        break;
    default:
        alertMessage = consoleMessage = "We are sorry but you entered a WRONG command, try again tho!'ntyped: " + codePrompt;
        break;
    }
    alert(alertMessage);
    console.log(consoleMessage);
});

我想你是在尝试这样做

var commands = {
    hide: 'hide()',
    hideBg: 'hide(background)'
};
var codePrompt = prompt("Set the code in the command line."),
    alertMessage;
switch (codePrompt) {
    case commands.hide:
        alertMessage = "Hiding elements...";
        break;
    case commands.hideBg:
        alertMessage = "Hiding Background...";
        break;
    default:
        alertMessage = "WRONG command";
        break;
    }
}

但是,您也可以使用

var commands = {
    'hide()': "Hiding elements...",
    'hide(background)': "Hiding Background..."
};
var codePrompt = prompt("Set the code in the command line.");
var alertMessage = commands[codePrompt] || "WRONG command";

我猜你也想运行一些功能:

var commands = {
    'hide()': {
        text: "Hiding elements...",
        funcion: someFunctionToHide
    },
    'hide(background)': {
        text: "Hiding Background...",
        funcion: someFunctionToHideBackground
    }
};
var codePrompt = prompt("Set the code in the command line."),
    command = commands[codePrompt];
if(!command) {
    alertMessage = "WRONG command";
} else {
    alertMessage = command.text;
    command.function();
}

switch通过使用身份操作符===将打开的值与每种可能的情况进行比较来进行操作。这意味着您可以在case中放入一个数组,并且它将按照指定的方式工作(但对于数组来说肯定不是很直观):

var x = [1];
var a = [1];
switch (x) {
    case [1]: alert("it's [1]!"); break;
    case a: alert("it's a!"); break;
    case x: alert("it's x!"); break;
}

这将提示"它是x!",而您可能期望前面两种情况中的任何一种都"足够好"来触发。但这就是===的工作方式:

[1] === x   // false
a === x     // true
x === x     // true

因此,虽然你可以在技术上使用数组,但在实践中,它实际上是非常不寻常的情况下,这样做是有用的。

回到你的代码,因为你感兴趣的值是字符串,似乎使用一个简单的对象作为映射会做得很好:

var commands = {
    "hide()": {
        alert: "Hiding elements...",
        console: "Blah blah"
    }.
    "hide(background)": {
        alert: "Hiding background...",
        console: "Blah blah"
    }.
};
var fallback = {
    alert: "Sorry, wrong command",
    console: "Sorry, wrong command"
};

允许你写

var result = commands[input] || fallback;
alert(result.alert);
console.log(result.console);