admin管理员组

文章数量:1193346

I have the following code

   switch (attr.templateType) {

      case 'text': return tpl_default; break;
      case 'currency': return tpl_currency; break;
      case 'percentage': return tpl_percentage; break;
      case 'latlong': return tpl_latlong; break;
      case 'tel': return tpl_phone; break;
      case 'number': return tpl_number; break;
      case 'address': return tpl_address; break;
      case 'date': return tpl_date; break;
      case 'permissions': return tpl_permissions; break;
      case 'pagination': return tpl_pagination; break;
      case 'time': return tpl_time; break;
      case 'notEmpty': return tpl_notEmpty; break;

      default: return tpl_default; break;
    }

and JavaScript lint tells me "unreachable code detected" for ALL the breaks. If i take out the breaks, lint has no errors.

does anyone know why? The code works and with out any errors.

I have the following code

   switch (attr.templateType) {

      case 'text': return tpl_default; break;
      case 'currency': return tpl_currency; break;
      case 'percentage': return tpl_percentage; break;
      case 'latlong': return tpl_latlong; break;
      case 'tel': return tpl_phone; break;
      case 'number': return tpl_number; break;
      case 'address': return tpl_address; break;
      case 'date': return tpl_date; break;
      case 'permissions': return tpl_permissions; break;
      case 'pagination': return tpl_pagination; break;
      case 'time': return tpl_time; break;
      case 'notEmpty': return tpl_notEmpty; break;

      default: return tpl_default; break;
    }

and JavaScript lint tells me "unreachable code detected" for ALL the breaks. If i take out the breaks, lint has no errors.

does anyone know why? The code works and with out any errors.

Share Improve this question asked Jul 5, 2017 at 1:46 user2062455user2062455 4211 gold badge5 silver badges16 bronze badges 1
  • Who is it returning to? – cs95 Commented Jul 5, 2017 at 1:50
Add a comment  | 

2 Answers 2

Reset to default 22

why is break required after return? switch will return and break will never execute, that is why it is unreachable.

you don't need break because you are returning out of the function. No code will execute after the return

本文标签: javascript switch break unreachable code detectedStack Overflow