Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

有关JS小程序的疑惑

在看js高级程序设计这本书 看到书中的一个小例子有点不懂这个例子意义在哪 因为懂c++ 所以明白这个switch 但是由于js中 case值可以是字符串 变量 所以这个例子不太明白

switch ("hello world") {
            case "hello" + " world": 
                alert("Greeting was found.");
                break;
            case "goodbye": 
                alert("Closing was found.");
                break;
            default: 
                alert("Unexpected message was found.");
        }

我不太明白 switch里面的的hello world是什么意思 感觉这样的话 岂不是永远会输出Greeting was found了?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

汗颜,

这例子唯一的意义是
告诉我们 JavaScript

  • case 里面是可以 用表达式的,可以用变量的

  • switch 里面也是可以用const char*字符串的,尽管这毫无意义

因为这是解释性语言,不需要编译,所以就可以这样。

这还什么《高级JS程序设计》,这些书的作者,真会框钱。

一个设计时间才十来天的语言,有这些奇怪的特性是正常的。

C++ 里面 switch 只能为 数字型枚举


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...