我可以在有条件的交换机上实现/放置数组吗?

时间:2014-05-30 19:26:57

标签: javascript jquery

当我想到一个奇怪的想法时,我正在构建我的代码,我可以在交换机中实现/放置一个数组吗?

我的意思是,如何让codeHide 案例有效?用这段代码它不起作用。

当我要求设置命令并将hide()codeHide[0] codeHide放在switch数组)时,我想codeHidealert case(我的if语句)并返回alertMessage告诉我该特定数组元素的hide(background)

如果我将codeHide[1]codeHide codeHide放在alert数组上)我想切换alertMessage case else(我的if语句)并返回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); }); (在is-statement中)。

希望你理解我。

这样做是不行的,我认为这是因为" {{1}}"。

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

{{1}}

2 个答案:

答案 0 :(得分:2)

我认为你正在尝试像

这样的东西
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();
}

答案 1 :(得分:1)

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);
相关问题