【发布时间】:2018-02-18 21:58:48
【问题描述】:
我的代码不是很长,所以我把它全部粘贴在这里。
代码不完整,但是当我运行它时,它首先跳转到它应该的 case “start”,然后跳转到 case “end”。我可以看到它,因为它会打印两个块的控制台日志文本。为什么要跳到“结束”案例?
<html>
<body>
<script>
function stepStream(stream,step){
switch (stream[step]){
case "start":
console.log("Started reading stream...");
case "end":
var success = "Finished reading dataStream.";
console.log(success);
return success;
default:
throw "Data stream format is bad";
case "gesture":
console.log("Running case gesture! But why?");
step+=1;
stepStream(stream,step);
case "say":
step+=1;
stepStream(stream,step);
case "sleep":
step+=1;
stepStream(stream,step);
}
}
var sentence1 = "Where are my bananas? I thought you put them in my bag?";
var sentence2 = "This is a rather irritating situattion.";
var dataStream = ["start","gesture","banzai","sleep",1.0,"say",sentence1,
"say",sentence2,"gesture","kubikasige","end"];
stepStream(dataStream,0);//Second parameter sets where to start reading the dataStream.
</script>
</body>
</html>
【问题讨论】:
-
您需要在每个
case的末尾添加一个break(已经以return或throw结尾的除外):developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。
标签: javascript switch-statement