在 JavaScript 中切换意外标记

发布于 2024-12-28 12:12:00 字数 600 浏览 2 评论 0原文

我在以下代码中发现了意外的标记。

        switch ( hobby ) {
            case =" painting ":
                message = "Van Gogh was good at that.";
                    break;
            case =" drawing ":
                message = "Hey! Van Gogh did that too.";
                    break;
            case =" playing guitar ":
                message = "Bob Dylan played guitar.";
                    break;
            case =" sleeping in ":
                message = "My favorite hobby in the winter.";
                    break;

            default
        }

有人看到我缺少什么吗?

I am finding an unexpected token in the following code.

        switch ( hobby ) {
            case =" painting ":
                message = "Van Gogh was good at that.";
                    break;
            case =" drawing ":
                message = "Hey! Van Gogh did that too.";
                    break;
            case =" playing guitar ":
                message = "Bob Dylan played guitar.";
                    break;
            case =" sleeping in ":
                message = "My favorite hobby in the winter.";
                    break;

            default
        }

Does anyone see what I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2025-01-04 12:12:00

你的语法已经关闭。删除 case 后面的 =。此外,您还需要在 default 之后添加 :

有关 switch 语句的更多信息,请参阅此处

Your syntax is off. Remove the = after cases. Also, you'll need to put a : after default.

See here for more about switch statements.

画骨成沙 2025-01-04 12:12:00

您可以利用函数语义来避免必须 break 脱离每个 case

var message = (function(){
  switch (hobby) {
    case 'painting':        return 'Van Gogh was good at that.'
    case 'drawing':         return 'Hey! Van Gogh did that too.'
    case 'playing guitar':  return 'Bob Dylan played guitar.'
    case 'sleeping in':     return 'My favorite hobby in the winter.'
  }
}())

不过,表达此逻辑的最佳方式可能如下所示:

var messages = {
  'painting':       'Van Gogh was good at that.',
  'drawing':        'Hey! Van Gogh did that too.',
  'playing guitar': 'Bob Dylan played guitar.',
  'sleeping in':    'My favorite hobby in the winter.'
}
var message = messages[hobby]

You can take advantage of function semantics to avoid having to break out of each case:

var message = (function(){
  switch (hobby) {
    case 'painting':        return 'Van Gogh was good at that.'
    case 'drawing':         return 'Hey! Van Gogh did that too.'
    case 'playing guitar':  return 'Bob Dylan played guitar.'
    case 'sleeping in':     return 'My favorite hobby in the winter.'
  }
}())

The best way to express this logic, though, is probably something like the following:

var messages = {
  'painting':       'Van Gogh was good at that.',
  'drawing':        'Hey! Van Gogh did that too.',
  'playing guitar': 'Bob Dylan played guitar.',
  'sleeping in':    'My favorite hobby in the winter.'
}
var message = messages[hobby]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文