JavaScript Patterns 2.6 switch Pattern

Principle

? Aligning each case with switch(an exception to the curly braces indentation rule).

? Indenting the code within each case.

? Ending each case with a clear break;.

? Avoiding fall-throughs (when you omit the break intentionally). If you‘re absolutely convinced that a fall-through is the best approach, make sure you document such cases, because they might look like errors to the readers of your code.

? Ending the switch with a default: to make sure there‘s always a sane result even if none of the cases matched.

JavaScript Patterns 2.6 switch Pattern
var inspect_me = 0,
result = ‘‘;

switch (inspect_me) {
   case 0:
      result = "zero";
      break;
   case 1:
      result = "one";
      break;
   default:
      result = "unknown";
}
JavaScript Patterns 2.6 switch Pattern

JavaScript Patterns 2.6 switch Pattern,布布扣,bubuko.com

JavaScript Patterns 2.6 switch Pattern

上一篇:javascript中事件概述


下一篇:java之插入排序