在 JavaScript 函数中切换大小写

Switch case in a JavaScript function

我有一个函数需要挑选水果的颜色,我选择了 switch case 语句。问题是我不确定如何从语句中得到结果。也许我应该改用 if/else 语句?

代码如下:

function fruitColor(fruit) {
    switch(color) {
        case "apple" : green;
            break;
        case "banana" : yellow;
            break;
        case "kiwi" : green;
            break;
        case "plum" : red;
            break;
    }
}

var result = fruitColor(plum);

我无法得到结果,我不确定我是否需要 'return' 值或类似的值。

首先,在 switch 语句中,你必须使用水果参数,而不是颜色,然后你需要一个变量来存储你的选择:

function fruitColor(fruit) {
    switch(fruit) {
        case "apple" : result = green;
            break;
        case "banana" : result = yellow;
            break;
        case "kiwi" : result = green;
            break;
        case "plum" : result = red;
            break;
    }
    return result;
}

var result = fruitColor(plum);

The return statement ends function execution and specifies a value to be returned to the function caller. return MDN

除了不返回值外,这里还有一些失误。 return 是从函数发回值的工具,但为了实现这一点,不会出现任何错误。就目前而言,变量 color 在 switch 语句中使用,但它不存在,可能是因为它应该是 fruit,反之亦然。此外,case 语句的结果代码中的值基本上只是对变量的引用,如果没有名为 green 的变量,那么它就是未定义的。也许你的意思是 "green".

function fruitColor(fruit) {
 //note that there was no value color here, there was only the accepted
 //parameter fruit, which should either be used or changed to color
 switch(color) {
  case "apple" : 
   //needs quotes, green with no quotes is a variable reference
   "green";
   //note that simply using the string "green" doesn't accomplish anything though
   //what we really need to do is send a value back, and in JavaScript you
   //use the return keyword for that followed by the returning value
   return "green";//like this
    break;
  case "banana" : "yellow";//^
    break;
  case "kiwi" : "green";//^
    break;
  case "plum" : "red";//^
    break;
 }
}
var result = fruitColor("plum");//needs quotes, plum would be a variable refernce

就我个人而言,我更喜欢字典来完成这类工作。

var fruitColors = {
 apple : "green",
 banana : "yellow",
 kiwi : "green",
 plum : "red"
};
var plumColor = fruitColors["plum"];//red

编码时,您总是希望代码尽可能保持高性能。既然我说了,让我给你一些解决这类问题的选择:

首先,让我们使用您当前的解决方案并使其发挥作用。

function fruitColor(fruit) {
    switch(color) {
        case "apple" :
            return 'green';
            break;
        case "banana" :
            return 'yellow';
            break;
        case "kiwi" :
            return 'green'
            break;
        case "plum" :
            return 'red';
            break;
    }
}

var result = fruitColor(plum);

这个使用了您的 switch 构造,并且 returns 过早地工作了。

但是,这不是解决此类问题的最佳方法,因为它会生成代码分叉,这意味着需要更多内存来存储和评估您的代码。另一种方法是使用带有水果和颜色的对象。

function fruitColor(fruit) {
    var fruits = {
        apple  : 'green',
        banana : 'yellow',
        kiwi   : 'green',
        plum   : 'red'
    };
    return fruits[fruit] || 'not found';
}

var result = fruitColor('plum');

此代码依赖于内存数据库,运行速度快且分叉较少,但它也依赖于搜索。

我想跟进

从这里开始,我想强调如何将参数放入 switch 语句中。参数变量 fruitcolor 接受一个参数。如果要将函数 fruitColor 中的参数带入 switch 语句,请使用相同的参数变量名称。即,两者都使用 fruit,或者两者都使用 color

function fruitColor(fruit) {

    // Note that there was no value color here. There was only the accepted
    // parameter fruit, which should either be used or changed to color
    switch(color) {
        case "apple":
            "green";
            return "green"; // Like this
            break;
        case "banana": "yellow"; // ^
            break;
        case "kiwi": "green"; // ^
            break;
        case "plum": "red"; // ^
            break;
    }
}
var result = fruitColor("plum");

这是我对 switch 构造的函数实现:

const fruitColor = fruit =>
  ({ apple: "green", banana: "yellow", kiwi: "green", plum: "red" }[fruit] ||
  "Nothing");