JavaScript切换功能

时间:2015-02-24 20:24:20

标签: javascript function switch-statement

我正在尝试运行此代码,但它给出了一个错误,说“意外的输入结束”。你能帮忙吗?提前谢谢。

var getReview = function (movie) {
switch (movie) {
case "Toy Story 2":
    return "Great story. Mean prospector.";
    break;
case "Finding Nemo" :
    return "Cool animation, and funny turtles.";
    break;
case "The Lion King" :
    return "Great songs.";
    break;
default : 
    return "I dont know!"; 
    break;
};

1 个答案:

答案 0 :(得分:2)

您的功能似乎缺少结束括号:

var getReview = function (movie) {
    switch (movie) {
    case "Toy Story 2":
        return "Great story. Mean prospector.";
        break;
    case "Finding Nemo" :
        return "Cool animation, and funny turtles.";
        break;
    case "The Lion King" :
        return "Great songs.";
        break;
    default : 
        return "I dont know!"; 
        break;
    };
};  // <-- here

另请注意,在break块结尾处return或分号后面switch是没有意义的。您可以将上述内容减少到:

var getReview = function (movie) {
    switch (movie) {
    case "Toy Story 2":
        return "Great story. Mean prospector.";
    case "Finding Nemo" :
        return "Cool animation, and funny turtles.";
    case "The Lion King" :
        return "Great songs.";
    default : 
        return "I dont know!"; 
    }
};