1"use strict";(self.webpackChunkpjchender_notes=self.webpackChunkpjchender_notes||[]).push([[21051],{28453:(e,s,n)=>{n.d(s,{R:()=>i,x:()=>r});var t=n(96540);const c={},a=t.createContext(c);function i(e){const s=t.useContext(a);return t.useMemo((function(){return"function"==typeof e?e(s):{...s,...e}}),[s,e])}function r(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(c):e.components||c:i(e.components),t.createElement(a.Provider,{value:s},e.children)}},60843:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>l,contentTitle:()=>r,default:()=>h,frontMatter:()=>i,metadata:()=>t,toc:()=>o});const t=JSON.parse('{"id":"javascript/js-switch-case","title":"[JS] Switch Case \u7684\u4f7f\u7528","description":"keywords: \u6d41\u7a0b\u63a7\u5236","source":"@site/docs/javascript/[JS] Switch Case.md","sourceDirName":"javascript","slug":"/javascript/js-switch-case","permalink":"/javascript/js-switch-case","draft":false,"unlisted":false,"editUrl":"https://github.com/pjchender/pjchender.github.io/tree/main/docs/javascript/[JS] Switch Case.md","tags":[],"version":"current","lastUpdatedAt":1775885633000,"frontMatter":{"id":"js-switch-case","title":"[JS] Switch Case \u7684\u4f7f\u7528","sidebar_label":"[JS] switch case","keywords":["JavaScript"]},"sidebar":"docs","previous":{"title":"[JS] Promise","permalink":"/javascript/js-promise"},"next":{"title":"[JS] symbols","permalink":"/javascript/js-symbols"}}');var c=n(74848),a=n(28453);const i={id:"js-switch-case",title:"[JS] Switch Case \u7684\u4f7f\u7528",sidebar_label:"[JS] switch case",keywords:["JavaScript"]},r=void 0,l={},o=[{value:"keywords: \u6d41\u7a0b\u63a7\u5236",id:"keywords-\u6d41\u7a0b\u63a7\u5236",level:5},{value:"\u51fd\u5f0f\u76f4\u63a5\u56de\u50b3",id:"\u51fd\u5f0f\u76f4\u63a5\u56de\u50b3",level:2},{value:"\u4e0d\u7528 Switch \u7684\u65b9\u5f0f",id:"\u4e0d\u7528-switch-\u7684\u65b9\u5f0f",level:2},{value:"switch \u548c if \u5728\u5404\u60c5\u6cc1\u4e0b\u7684\u6548\u80fd\u6bd4\u8f03",id:"switch-\u548c-if-\u5728\u5404\u60c5\u6cc1\u4e0b\u7684\u6548\u80fd\u6bd4\u8f03",level:2}];function d(e){const s={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h5:"h5",li:"li",p:"p",pre:"pre",ul:"ul",...(0,a.R)(),...e.components};return(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(s.h5,{id:"keywords-\u6d41\u7a0b\u63a7\u5236",children:"keywords: \u6d41\u7a0b\u63a7\u5236"}),"\n",(0,c.jsxs)(s.blockquote,{children:["\n",(0,c.jsxs)(s.p,{children:[(0,c.jsx)(s.a,{href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch",children:"Switch Statement"})," @ MDN"]}),"\n"]}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-javascript",children:"const action = 'say_hello';\nswitch (action) {\n case 'say_hello': {\n // \u662f\u53bb\u5224\u65b7 action === 'say_hello'\n let message = 'hello';\n console.log('message', message);\n break; // \u5982\u679c\u9019\u88e1\u6c92\u6709 break\uff0c\u5247\u6703\u7e7c\u7e8c\u8dd1\u5f8c\u9762\u7684 statement\uff08\u4e0d\u9700\u8981\u5224\u65b7\u6709\u6c92\u6709\u7b26\u5408\u689d\u4ef6\uff09\n }\n case 'say_hi': {\n // \u662f\u53bb\u5224\u65b7 action === 'say_hi'\n let message = 'hi';\n console.log('message', message);\n break;\n }\n default: {\n break;\n }\n}\n"})}),"\n",(0,c.jsxs)(s.ul,{children:["\n",(0,c.jsxs)(s.li,{children:["Switch statement \u505a\u7684\u4e8b\u662f\u628a\u5e36\u5165 ",(0,c.jsx)(s.code,{children:"switch(action)"})," \u4e2d\u7684\u53c3\u6578 ",(0,c.jsx)(s.code,{children:"action"})," \uff0c\u53bb\u548c ",(0,c.jsx)(s.code,{children:"case 'say_hello'"})," \u5f8c\u9762\u7684 expression \u7528 strict comparison\uff08",(0,c.jsx)(s.code,{children:"==="}),"\uff09\u7684\u65b9\u5f0f\u4f86\u5224\u65b7\uff0c\u5982\u679c\u5169\u500b\u76f8\u7b49\uff0c\u5c31\u6703\u9032\u5230 ",(0,c.jsx)(s.code,{children:"case"})," \u5f8c\u9762\u7684 statement\u3002"]}),"\n",(0,c.jsxs)(s.li,{children:["\u4e00\u65e6\u9032\u5165\u67d0\u4e00\u500b case \u5f8c\u7684 statement\uff0c\u5c31\u9700\u8981\u4f7f\u7528 ",(0,c.jsx)(s.code,{children:"break"})," \u9032\u884c\u7d42\u6b62\uff0c\u6c92\u6709\u4f7f\u7528 ",(0,c.jsx)(s.code,{children:"break"})," \u7684\u8a71\uff0c\u7b26\u5408\u8a72\u689d\u4ef6\u5f8c\u9762\u7684\u6240\u6709 statement\uff08\u4e0d\u8ad6\u6709\u6c92\u6709\u7b26\u5408\u689d\u4ef6\uff09\u4e5f\u90fd\u6703\u88ab\u57f7\u884c\u5230"]}),"\n",(0,c.jsxs)(s.li,{children:[(0,c.jsx)(s.code,{children:"default"})," \u4e00\u822c\u6703\u653e\u5728\u6700\u5f8c\uff0c\u4f46\u9019\u4e0d\u662f\u786c\u6027\u898f\u5b9a"]}),"\n"]}),"\n",(0,c.jsx)(s.h2,{id:"\u51fd\u5f0f\u76f4\u63a5\u56de\u50b3",children:"\u51fd\u5f0f\u76f4\u63a5\u56de\u50b3"}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-js",children:"const COLLECT_ISSUE_SEVERITY = {\n low: 1,\n medium: 2,\n high: 3,\n};\n\nconst getCollectIssueSeverity = (severityCode) => {\n switch (severityCode) {\n case COLLECT_ISSUE_SEVERITY.low:\n return 'Low';\n case COLLECT_ISSUE_SEVERITY.medium:\n return 'Medium';\n case COLLECT_ISSUE_SEVERITY.high:\n return 'High';\n default:\n return 'Indeterminate';\n }\n};\n"})}),"\n",(0,c.jsxs)(s.blockquote,{children:["\n",(0,c.jsxs)(s.p,{children:["\u5982\u679c\u6703\u5728 case \u4e2d\u4f7f\u7528 ",(0,c.jsx)(s.code,{children:"let"})," \u6216 ",(0,c.jsx)(s.code,{children:"const"})," \u5b9a\u7fa9\u8b8a\u6578\u7684\u8a71\uff0c\u9700\u8981\u5728 ",(0,c.jsx)(s.code,{children:"case"})," \u5f8c\u52a0\u4e0a\u642d\u62ec\u865f ",(0,c.jsx)(s.code,{children:"{}"}),"\uff0c\u53c3\u8003\uff1a",(0,c.jsx)(s.a,{href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#Block-scope_variables_within_switch_statements",children:"Block-scope variables within switch statements"})," @ MDN"]}),"\n"]}),"\n",(0,c.jsx)(s.h2,{id:"\u4e0d\u7528-switch-\u7684\u65b9\u5f0f",children:"\u4e0d\u7528 Switch \u7684\u65b9\u5f0f"}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-js",children:"const STATUS = {\n indeterminate: 0,\n success: 1,\n partialSuccess: 2,\n fail: 3,\n default: 0,\n};\n\nfunction getSuccessStatus(statusCode) {\n const statusSupplement = {\n [STATUS.indeterminate]: {\n title: 'Indeterminate',\n color: 'blue',\n },\n [STATUS.success]: {\n title: 'Success',\n color: 'green',\n },\n [STATUS.partialSuccess]: {\n title: 'Partial Success',\n color: 'yellow',\n },\n [STATUS.fail]: {\n title: 'Fail',\n color: 'red',\n },\n default: {\n title: 'Default Value',\n color: 'gray',\n },\n };\n\n return statusSupplement[statusCode] || statusSupplement.default;\n}\n\ngetSuccessStatus(5);\n"})}
1),"\n",(0,c.jsx)("iframe",{height:"400px",width:"100%",src:"https://repl.it/@PJCHENder/Alternative-of-switch-statement-in-JavaScript?lite=true",scrolling:"no",frameborder:"no",allowtransparency:"true",allowfullscreen:"true",sandbox:"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"}),"\n",(0,c.jsxs)(s.blockquote,{children:["\n",(0,c.jsx)(s.p,{children:(0,c.jsx)(s.a,{href:"https://enmascript.com/articles/2018/10/22/why-I-prefer-objects-over-switch-statements",children:"Why I prefer objects over switch statements"})}),"\n"]}),"\n",(0,c.jsx)(s.h2,{id:"switch-\u548c-if-\u5728\u5404\u60c5\u6cc1\u4e0b\u7684\u6548\u80fd\u6bd4\u8f03",children:"switch \u548c if \u5728\u5404\u60c5\u6cc1\u4e0b\u7684\u6548\u80fd\u6bd4\u8f03"}),"\n",(0,c.jsxs)(s.blockquote,{children:["\n",(0,c.jsxs)(s.p,{children:[(0,c.jsx)(s.a,{href:"https://stackoverflow.com/questions/6665997/switch-statement-for-greater-than-less-than",children:"Switch statement for greater-than/less-than"})," @ StackOverflow"]}),"\n"]}),"\n",(0,c.jsx)(s.p,{children:"\u4e00\u822c\u4f86\u8aaa if \u7684\u6548\u80fd\u90fd\u6bd4 switch \u597d\uff1a"}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-js",children:"if (val < 1000) { /*do something */ } else\n"})}),"\n",(0,c.jsx)(s.p,{children:"switch immediate \u7684\u6548\u80fd\u6b21\u597d\uff1a"}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-js",children:"switch (Math.floor(val/1000)) {\n case 0: /* do something */ break;\n case 1: /* do something */ break;\n ...\n}\n"})}),"\n",(0,c.jsxs)(s.p,{children:["\u5343\u842c\u4e0d\u8981\u7528 ",(0,c.jsx)(s.code,{children:"switch(true)"})," \u9019\u6a23\u7684\u5beb\u6cd5\uff1a"]}),"\n",(0,c.jsx)(s.pre,{children:(0,c.jsx)(s.code,{className:"language-js",children:"switch (true) {\n case (0 <= val && val < 1000): /* do something */ break;\n ...\n}\n"})})]})}function h(e={}){const{wrapper:s}={...(0,a.R)(),...e.components};return s?(0,c.jsx)(s,{...e,children:(0,c.jsx)(d,{...e})}):d(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.