{"id":52922,"date":"2024-12-03T13:40:42","date_gmt":"2024-12-03T05:40:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52922\/"},"modified":"2024-12-03T13:40:42","modified_gmt":"2024-12-03T05:40:42","slug":"%e4%ba%86%e8%a7%a3-javascript-%e4%b8%ad%e7%9a%84switch%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52922\/","title":{"rendered":"\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324465896743097dc3eb8.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5\u63d2\u56fe\" alt=\"\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5\u63d2\u56fe\" \/><\/p>\n<p><strong>switch<\/strong> \u8bed\u53e5\u662f\u4e00\u79cd\u63a7\u5236\u7ed3\u6784\uff0c\u5141\u8bb8\u60a8\u6839\u636e\u8868\u8fbe\u5f0f\u7684\u503c\u6267\u884c\u591a\u4e2a\u4ee3\u7801\u5757\u4e4b\u4e00\u3002\u5b83\u662f\u591a\u4e2a <strong>if-else<\/strong> \u8bed\u53e5\u7684\u66f4\u6e05\u6670\u3001\u66f4\u6613\u8bfb\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u8ba9\u6211\u4eec\u7528\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5206\u89e3\u5b83\u3002<\/p>\n<h3> <strong>\u8bed\u6cd5<\/strong> <\/h3>\n<p><strong>switch<\/strong> \u8bed\u53e5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>switch (expression) {\n  case value1:\n    \/\/ code to execute if expression === value1\n    break;\n  case value2:\n    \/\/ code to execute if expression === value2\n    break;\n  \/\/ ... more cases ...\n  default:\n    \/\/ code to execute if none of the cases match\n}\n\n<\/pre>\n<h3> <strong>\u4e00\u4e2a\u4f8b\u5b50<\/strong> <\/h3>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u4f7f\u7528 <strong>switch<\/strong> \u8bed\u53e5\u6839\u636e\u6570\u5b57\u786e\u5b9a\u661f\u671f\u51e0\u3002<\/p>\n<pre>let day = 3;\nlet dayname;\n\nswitch (day) {\n  case 1:\n    dayname = \"monday\";\n    break;\n  case 2:\n    dayname = \"tuesday\";\n    break;\n  case 3:\n    dayname = \"wednesday\";\n    break;\n  case 4:\n    dayname = \"thursday\";\n    break;\n  case 5:\n    dayname = \"friday\";\n    break;\n  case 6:\n    dayname = \"saturday\";\n    break;\n  case 7:\n    dayname = \"sunday\";\n    break;\n  default:\n    dayname = \"invalid day\";\n}\n\nconsole.log(dayname); \/\/ output: wednesday\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li><strong>switch<\/strong> \u8bed\u53e5\u8bc4\u4f30 <strong>day<\/strong> \u7684\u503c\u3002<\/li>\n<li>\u5982\u679c <strong>day<\/strong> \u4e3a <strong>1<\/strong>\uff0c\u5219\u5c06 <strong>dayname<\/strong> \u8bbe\u7f6e\u4e3a\u201c\u661f\u671f\u4e00\u201d\u3002<\/li>\n<li>\u5982\u679c <strong>day<\/strong> \u4e3a <strong>2<\/strong>\uff0c\u5219\u5c06 <strong>dayname<\/strong> \u8bbe\u7f6e\u4e3a\u201c\u661f\u671f\u4e8c\u201d\u3002<\/li>\n<li>\u7b49\u7b49&#8230;<\/li>\n<li>\u5982\u679c <strong>day<\/strong> \u4e0e\u4efb\u4f55\u60c5\u51b5\u90fd\u4e0d\u5339\u914d\uff0c\u5219\u4f1a\u5c06 <strong>dayname<\/strong> \u8bbe\u7f6e\u4e3a\u201c\u65e0\u6548\u65e5\u671f\u201d\u3002<\/li>\n<\/ul>\n<h3> <strong>\u6848\u4f8b\u5206\u7ec4<\/strong> <\/h3>\n<p>\u5982\u679c\u60a8\u60f3\u5bf9\u4e0d\u540c\u7684\u503c\u6267\u884c\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a <strong>case<\/strong> \u8bed\u53e5\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let fruit = \"apple\";\nlet category;\n\nswitch (fruit) {\n  case \"apple\":\n  case \"banana\":\n  case \"orange\":\n    category = \"fruit\";\n    break;\n  case \"carrot\":\n  case \"broccoli\":\n    category = \"vegetable\";\n    break;\n  default:\n    category = \"unknown\";\n}\n\nconsole.log(category); \/\/ output: fruit\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c<strong>\u6c34\u679c<\/strong>\u662f\u201c\u82f9\u679c\u201d\u3001\u201c\u9999\u8549\u201d\u6216\u201c\u6a59\u5b50\u201d\uff0c\u5219\u4f1a\u5c06<strong>\u7c7b\u522b<\/strong>\u8bbe\u7f6e\u4e3a\u201c\u6c34\u679c\u201d\u3002<\/li>\n<li>\u5982\u679c<strong>\u6c34\u679c<\/strong>\u662f\u201c\u80e1\u841d\u535c\u201d\u6216\u201c\u897f\u5170\u82b1\u201d\uff0c\u5219\u4f1a\u5c06<strong>\u7c7b\u522b<\/strong>\u8bbe\u7f6e\u4e3a\u201c\u852c\u83dc\u201d\u3002<\/li>\n<li>\u5982\u679c<strong>\u6c34\u679c<\/strong>\u4e0e\u4efb\u4f55\u60c5\u51b5\u90fd\u4e0d\u5339\u914d\uff0c\u5219\u4f1a\u5c06<strong>\u7c7b\u522b<\/strong>\u8bbe\u7f6e\u4e3a\u201c\u672a\u77e5\u201d\u3002<\/li>\n<\/ul>\n<h3> <strong>\u7c7b\u578b\u5f88\u91cd\u8981<\/strong> <\/h3>\n<p><strong>switch<\/strong> \u8bed\u53e5\u4f7f\u7528\u4e25\u683c\u6bd4\u8f83 (<strong>===<\/strong>) \u5c06\u8868\u8fbe\u5f0f\u4e0e case \u503c\u8fdb\u884c\u5339\u914d\u3002\u8fd9\u610f\u5473\u7740\u503c\u548c\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let value = \"1\";\n\nswitch (value) {\n  case 1:\n    console.log(\"Number 1\");\n    break;\n  case \"1\":\n    console.log(\"String 1\");\n    break;\n  default:\n    console.log(\"Unknown\");\n}\n\n\/\/ Output: String 1\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li><strong>switch<\/strong> \u8bed\u53e5\u4f7f\u7528\u4e25\u683c\u6bd4\u8f83\u5c06 <strong>value<\/strong> \u4e0e case \u503c\u8fdb\u884c\u5339\u914d\u3002<\/li>\n<li>\u7531\u4e8e <strong>value<\/strong> \u662f\u5b57\u7b26\u4e32 <strong>&#8220;1&#8221;<\/strong>\uff0c\u56e0\u6b64\u5b83\u4e0e\u5927\u5c0f\u5199 <strong>&#8220;1&#8221;<\/strong> \u5339\u914d\u5e76\u6253\u5370\u201cstring 1\u201d\u3002<\/li>\n<\/ul>\n<h3> <strong>\u603b\u7ed3<\/strong> <\/h3>\n<ul>\n<li> <strong>\u8bed\u6cd5<\/strong>\uff1a<strong>switch<\/strong> \u8bed\u53e5\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5e76\u6267\u884c\u76f8\u5e94\u7684 case \u5757\u3002<\/li>\n<li> <strong>\u793a\u4f8b<\/strong>\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <strong>switch<\/strong> \u8bed\u53e5\u6765\u786e\u5b9a\u661f\u671f\u51e0\u3002<\/li>\n<li> <strong>case \u5206\u7ec4<\/strong>\uff1a\u591a\u4e2a <strong>case<\/strong> \u8bed\u53e5\u53ef\u4ee5\u7ec4\u5408\u5728\u4e00\u8d77\u6765\u6267\u884c\u76f8\u540c\u7684\u4ee3\u7801\u3002<\/li>\n<li> <strong>\u7c7b\u578b\u5f88\u91cd\u8981<\/strong>\uff1a<strong>switch<\/strong> \u8bed\u53e5\u4f7f\u7528\u4e25\u683c\u6bd4\u8f83 (<strong>===<\/strong>) \u5c06\u8868\u8fbe\u5f0f\u4e0e\u5927\u5c0f\u5199\u503c\u8fdb\u884c\u5339\u914d\u3002<\/li>\n<\/ul>\n<h3> <strong>\u7ed3\u8bba<\/strong> <\/h3>\n<p><strong>switch<\/strong> \u8bed\u53e5\u662f\u5904\u7406 javascript \u4e2d\u591a\u4e2a\u6761\u4ef6\u7684\u5f3a\u5927\u4e14\u53ef\u8bfb\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4e86\u89e3\u5176\u8bed\u6cd5\u3001\u5982\u4f55\u5bf9\u6848\u4f8b\u8fdb\u884c\u5206\u7ec4\u4ee5\u53ca\u7c7b\u578b\u5339\u914d\u7684\u91cd\u8981\u6027\uff0c\u60a8\u5c06\u80fd\u591f\u7f16\u5199\u66f4\u9ad8\u6548\u4e14\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u4e0d\u65ad\u7ec3\u4e60\u548c\u63a2\u7d22\uff0c\u52a0\u6df1\u5bf9 javascript \u4e2d <strong>switch<\/strong> \u8bed\u53e5\u7684\u7406\u89e3\u3002<\/p>\n<p>\u8bf7\u7ee7\u7eed\u5173\u6ce8\u6709\u5173 javascript \u7684\u66f4\u591a\u6df1\u5165\u535a\u5ba2\uff01\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   FastAPI\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49Swagger\u54cd\u5e94\u7684\u5a92\u4f53\u7c7b\u578b\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5 \u6536\u85cf \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u4e86\u89e3 JavaScript \u4e2d\u7684\u201cswitch\u201d\u8bed\u53e5\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 switch \u8bed\u53e5\u662f\u4e00\u79cd\u63a7\u5236\u7ed3\u6784\uff0c\u5141\u8bb8\u60a8\u6839\u636e\u8868\u8fbe\u5f0f\u7684\u503c\u6267\u884c\u591a\u4e2a\u4ee3\u7801\u5757\u4e4b\u4e00\u3002\u5b83\u662f\u591a\u4e2a if-else \u8bed\u53e5\u7684\u66f4\u6e05\u6670\u3001\u66f4\u6613\u8bfb\u7684\u66ff\u4ee3\u65b9\u6848\u3002\u8ba9\u6211\u4eec\u7528\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5206\u89e3\u5b83\u3002 \u8bed\u6cd5 switch \u8bed\u53e5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a switch (expression) { case value1: \/\/ code to execute if expression === value1 break; case value2: \/\/ code to execute if expression === value2 break; \/\/ &#8230; more cases &#8230; default: \/\/ code to execute if none of the cases match [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52922","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52922"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52922\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}