具有返回和中断的开关盒

时间:2013-08-23 01:35:16

标签: javascript design-patterns

出于好奇,我经常会看到以下情况:

switch(something) {
    case 'alice':
        return something;
    break;
}

break似乎完全没必要,有什么理由让它在那里吗?

4 个答案:

答案 0 :(得分:24)

break;语句可能在引入return语句之前就已存在。因此,它已经变得多余,可以删除。

事实上,当您通过jslint运行该代码时,它会显示以下错误:

  

'return'后无法访问'break'。

是否注意这个建议取决于你;如果你在确定特定风格之前尝试了一些事情,那么在开发过程中它可能会有所帮助。

这是另一种写作风格,有些人认为这是一种更好的做法:

var retval = null;
switch (something) {
    case 'alice':
        retval = something;
        break;
    // ...
}
return retval;

答案 1 :(得分:5)

break告诉javascript停止评估switch块中的案例。代码执行继续超过结束switch括号。示例代码中的return语句确实会阻止其后的任何内容,包括其他case语句以及switch块之后的任何内容。

我在每种情况下都习惯性地提出break陈述。如果我写了一个没有break的案例,那么我将来可能会复制并粘贴代码块,缺少break语句就会变成这样的错误:

function whereLivesA(species){
  switch(species){
    case 'worms': 
      // Relying on return to prevent further code execution within the switch
      // block works but is ~bad~ smelly (according to plato :D)
      var habitat = 'dirt'
      return (species + ' live in ' + habitat);
    case 'bees':
      var habitat = 'hive';
      break;
  }
  // Stuff to do after the switch statement (unless you returned already)
  var str = species+' live in '+habitat;
  return str;
}
console.log('whereLivesA');
console.log(whereLivesA("worms"));
console.log(whereLivesA("bees"));
  /* Output:
    whereLivesA
    worms live in dirt
    bees live in hive
  */


function whereLivesB(species){
  switch(species){
    case "worms": 
      // what if future code changes remove `return` and don't add `break`?
      // return (species + ' live in ' + habitat)
      var habitat = 'dirt';
      // break;
    case "bees":
      var habitat = 'hive'
      break;
  }
  // Stuff to do after the switch statement (unless you returned already)
  var str = species+' live in '+habitat;
  return str;
}
console.log('whereLivesB');
console.log(whereLivesB("bees"));
console.log(whereLivesB("worms"));
  /* Output:
    whereLivesB
    bees live in hive
    worms live in hive
  */


function whereLivesCorrect(species){
  switch(species){
    case "worms": 
      var habitat = 'dirt';
      break;
    case "bees":
      var habitat = 'hive'
      break;
  }
  // Stuff to do after the switch statement (unless you returned already)
  var str = species+' live in '+habitat;
  return str;
}

console.log('whereLivesCorrect');
console.log(whereLivesCorrect("bees"));
console.log(whereLivesCorrect("worms"));
  /* Output:
    whereLivesCorrect
    bees live in hive
    worms live in dirt
  */

JS新手:如果您不想将其保存到文件并运行node filename,则可以按F12并将此脚本或其他自包含脚本粘贴到浏览器的控制台中以运行它。

如果您使用node.js,您还可以在命令行键入node以启动node控制台并将其粘贴到那里。

答案 2 :(得分:0)

break关键字用于结束语句,或退出循环,因此不会继续运行。

例如:

HTML

what's your age?: <input type="text" id="ageOf"><br>
<input type="submit" onSubmit="postReply();">
<div id="reply"></div>

JS

var userResponse = document.getElementById('ageOf');
var response = document.getElementById('reply');

function postReply() {
  switch(ageOf) {

    case 1: ageOf<18 {
      response.innerHTML = "You are still young.";
      break;
    }

    case 2: ageOf>18 && ageOf<45 {
      response.innerHTML = "You are getting up there...";
      break;
    }

    case 3: ageOf >= 45 {
      response.innerHTML = "You are over the hill!";
      break;
    }
}

所以在提交时,表单应该调用function postReply(),检查用户的答案,并根据值,它应该返回其中一个语句。

答案 3 :(得分:-3)

案件实际上非常必要

你有一个案子,那么你需要打破这种情况,否则其他案件也会被提起。

使用案例通常被认为是不好的做法,尽可能远离它们。

switch(casein){
case 1:{
break;
}
case 2:{
break
}
}
相关问题