首页 > 网站 > WEB开发 > 正文

JavaScript Patterns 2.6 switch Pattern

2024-04-27 14:23:46
字体:
来源:转载
供稿:网友

javaScript Patterns 2.6 switch Pattern

2014-05-21 22:03 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑

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.

var inspect_me = 0,result = '';switch (inspect_me) {   case 0:      result = "zero";      break;   case 1:      result = "one";      break;   default:      result = "unknown";}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表