{"id":1243,"date":"2022-06-04T10:09:35","date_gmt":"2022-06-04T02:09:35","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1243"},"modified":"2023-02-16T14:43:41","modified_gmt":"2023-02-16T06:43:41","slug":"25-switch-yu-ju-jie-shi-shuo-ming","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1243","title":{"rendered":"25 &#8211; switch \u8bed\u53e5\u89e3\u91ca\u8bf4\u660e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16543086251123.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/js-switch-statement-explained-101c\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/js-switch-statement-explained-101c<\/a><\/p>\n<h2><a id=\"%E4%BB%8B%E7%BB%8D\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ecb\u7ecd<\/h2>\n<p>switch \u662f\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\uff0c\u6839\u636e\u8868\u8fbe\u5f0f\u7684\u503c\u6765\u6267\u884c\u5bf9\u5e94\u7684\u8bed\u53e5\uff0c\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u4e3a\u591a\u5206\u652f if \u8bed\u53e5\u3002<\/p>\n<h2><a id=\"%E5%85%B3%E9%94%AE%E7%82%B9\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5173\u952e\u70b9<\/h2>\n<ol>\n<li>\u6267\u884c\u8868\u8fbe\u5f0f<\/li>\n<li>case \u5757<\/li>\n<li>\uff08\u53ef\u9009\uff09\u9ed8\u8ba4\u5757<\/li>\n<\/ol>\n<h2><a id=\"%E8%AF%AD%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8bed\u6cd5<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">switch (expressopm) {\n    case value1:\n        \/\/Statements executed when the\n        \/\/result of expression matches value1\n        break; \/\/ break from further evaluation\n    case value2:\n        \/\/Statements executed when the\n        \/\/result of expression matches value2\n        break;\n    case valueN:\n        \/\/Statements executed when the\n        \/\/result of expression matches valueN\n        break;\n    default:\n        \/\/Statements executed when none of\n        \/\/the values match the value of the expression\n        break;\n}\n<\/code><\/pre>\n<h2><a id=\"%E4%BE%8B%E5%AD%90\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f8b\u5b50<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const tellMeTheNumber = (num) =&gt; {\n    switch(num) {\n        case 1:\n            console.log(&quot;You are number one!&quot;);\n            break;\n        case 2:\n            console.log(&quot;Second is not a bad place to be.&quot;);\n            break;\n        case 3:\n            console.log(&quot;Three Three Three&quot;);\n            break;\n        case 4:\n            console.log(&quot;Quad&quot;);\n            break;\n        default:\n            console.log(&quot;I don't know who I am anymore?&quot;);\n            break;\n    }\n}\n\ntellMeTheNumber(4); \/\/ Quad\ntellMeTheNumber(1); \/\/ You are number one!\ntellMeTheNumber(1); \/\/ I don't know who I am anymore?\n<\/code><\/pre>\n<h2><a id=\"%E7%9C%81%E7%95%A5break%E4%BC%9A%E5%A6%82%E4%BD%95%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7701\u7565 break \u4f1a\u5982\u4f55\uff1f<\/h2>\n<p>\u5728 switch \u8bed\u53e5\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u7701\u7565\u4e86 break \u8bed\u53e5\uff0c\u6240\u6709\u7684\u8bed\u53e5\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const tellMeTheNumber = (num) =&gt; {\n    switch(num) {\n        case 1:\n            console.log(&quot;You are number one!&quot;);\n        case 2:\n            console.log(&quot;Second is not a bad place to be.&quot;);\n        case 3:\n            console.log(&quot;Three Three Three&quot;);\n        case 4:\n            console.log(&quot;Quad&quot;);\n        default:\n            console.log(&quot;I don't know who I am anymore?&quot;);\n            break;\n    }\n}\n\ntellMeTheNumber(1);\n\/\/ You are number one!\n\/\/ Second is not a bad place to be.\n\/\/ Three Three Three\n\/\/ Quad\n\/\/ I don't know who I am anymore?\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u867d\u7136\u6211\u4eec\u4f20\u5165 1 \u4f46\u8bed\u53e5\u4e2d\u90fd\u6ca1\u6709 break \u5173\u952e\u5b57\uff0c\u4f46\u662f\u4f9d\u65e7\u4f1a\u6267\u884c 2\uff0c3\uff0c4 \u548c \u9ed8\u8ba4\u8bed\u53e5\u3002<\/p>\n<h2><a id=\"%E5%88%86%E7%BB%84%E6%83%85%E5%86%B5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5206\u7ec4\u60c5\u51b5<\/h2>\n<p>\u5728 switch \u8bed\u53e5\u4e2d\u82e5\u591a\u4e2a\u6761\u4ef6\u9700\u8981\u6267\u884c\u76f8\u540c\u7684\u52a8\u4f5c\uff0c\u4e3a\u4e86\u907f\u514d\u4ee3\u7801\u5197\u4f59\u6211\u4eec\u53ef\u4ee5\u628a\u4ed6\u4eec\u5206\u7ec4\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const tellMeTheNumber = (num) =&gt; {\n    switch (num) {\n        case 1:\n        case 2:\n        case 3:\n            console.log(&quot;You are in top 3&quot;);\n            break;\n        case 4:\n            console.log(&quot;You did not make it this time&quot;);\n            break;\n        default:\n            console.log(&quot;I don't know who I am anymore?&quot;);\n            break;\n    }\n}\n\ntellMeTheNumber(2); \/\/ You are in top 3\ntellMeTheNumber(4); \/\/ You did not make it this time\ntellMeTheNumber(12); \/\/ I don't know who I am anymore?\n<\/code><\/pre>\n<p>1\uff0c2 \u6216 3 \u4f1a\u8f93\u51fa\u76f8\u540c\u7684\u4fe1\u606f\u3002<\/p>\n<h2><a id=\"%E4%B8%A5%E6%A0%BC%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%B5%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e25\u683c\u7c7b\u578b\u68c0\u6d4b<\/h2>\n<p>switch \u8bed\u53e5\u7684\u4e2d\u8868\u8fbe\u5f0f\u4e0e case \u8bed\u53e5\u7684\u6bd4\u8f83\u662f <code>====<\/code> \u6bd4\u8f83\u5176\u503c\u540c\u65f6\u4e5f\u6bd4\u8f83\u7c7b\u578b\uff0c\u6240\u4ee5\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4f20\u5165 <code>&quot;3&quot;<\/code> \u548c <code>3<\/code> \u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const tellMeTheNumber = (num) =&gt; {\n    switch (num) {\n        case 1:\n        case 2:\n        case 3:\n            console.log(&quot;You are in top 3&quot;);\n            break;\n        case 4:\n            console.log(&quot;You did not make it this time&quot;);\n            break;\n        default:\n            console.log(&quot;I don't know who I am anymore?&quot;);\n            break;\n    }\n}\n\ntellMeTheNumber(3); \/\/ You are in top 3\ntellMeTheNumber(&quot;3&quot;); \/\/ I don't know who I am anymore?\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u5b57\u7b26\u4e32 <code>&quot;3&quot;<\/code> \u4e0d\u4f1a\u5339\u914d\u4efb\u4f55 case \u6240\u4ee5 default \u4f1a\u6267\u884c\u3002<\/p>\n<h2><a id=\"%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5757\u7ea7\u4f5c\u7528\u57df<\/h2>\n<p>ES6 \u6216 ES2015 \u5f15\u5165 let \u548c const \u6765\u521b\u5efa\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u8bb0\u4f4f\u5982\u679c\u5728 switch \u8bed\u53e5\u4e2d\u4f7f\u7528\u5b83\u4eec\u9700\u8981\u591a\u52a0\u6ce8\u610f\u4e00\u4e9b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const action = 'say_hello';\nswitch (action) {\n  case 'say_hello':\n    let message = 'hello';\n    console.log(message);\n    break;\n  case 'say_hi':\n    let message = 'hi';\n    console.log(message);\n    break;\n  default:\n    console.log('Empty action received.');\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u62a5\u9519\uff1a<strong>Uncaught SyntaxError: Identifier &#8216;message&#8217; has already been declared<\/strong> \u5373\u4f7f message \u5728\u4e0d\u540c\u7684 case \u4e2d\u58f0\u660e\u4ed6\u4eec\u4f9d\u65e7\u662f\u5c5e\u4e8e\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u6240\u4ee5\u4ea7\u751f\u4e86\u51b2\u7a81\u3002<\/p>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u7a0d\u52a0\u53d8\u5316\uff0c\u5219\u4f1a\u6d88\u9664\u8be5\u62a5\u9519\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const action = 'say_hello';\nswitch (action) {\n  case 'say_hello': { \/\/ added brackets\n    let message = 'hello';\n    console.log(message);\n    break;\n  } \/\/ added brackets\n  case 'say_hi': { \/\/ added brackets\n    let message = 'hi';\n    console.log(message);\n    break;\n  } \/\/ added brackets\n  default: { \/\/ added brackets\n    console.log('Empty action received.');\n  } \/\/ added brackets\n}\n<\/code><\/pre>\n<p>\u6ce8\u610f\u4e0b\u9762\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\u79fb\u51fa say_hi \u4e2d\u7684 let \u540c\u65f6\u4f7f\u7528 <code>&quot;say_hello&quot;<\/code> \u8c03\u7528:<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const action = 'say_hello';\nswitch (action) {\n  case 'say_hello':\n    let message = 'hello';\n    console.log(message);\n    break;\n  case 'say_hi':\n    message = 'hi';\n    console.log(message);\n    break;\n  default:\n    console.log('Empty action received.');\n}\n<\/code><\/pre>\n<p>\u6b63\u5e38\u8f93\u51fa\uff1a<code>hello<\/code>\uff0c<\/p>\n<ol start=\"2\">\n<li>\u79fb\u51fa say_hi \u4e2d let \u540c\u65f6\u4f7f\u7528 <code>&quot;say_hi&quot;<\/code> \u8c03\u7528\uff1a<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const action = 'say_hi';\nswitch (action) {\n  case 'say_hello':\n    let message = 'hello';\n    console.log(message);\n    break;\n  case 'say_hi':\n    message = 'hi';\n    console.log(message);\n    break;\n  default:\n    console.log('Empty action received.');\n}\n<\/code><\/pre>\n<p>\u62a5\u9519\uff1a<strong>Uncaught ReferenceError: Cannot access &#8216;message&#8217; before initialization<\/strong>\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-1.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1ahttps:\/\/dev.to\/bhagatparwinder\/js-switch-statem&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[135,136],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-js-parwinder","tag-switch"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1243"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions\/1245"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}