{"id":4864,"date":"2023-01-05T11:07:29","date_gmt":"2023-01-05T03:07:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4864"},"modified":"2023-01-05T11:07:29","modified_gmt":"2023-01-05T03:07:29","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e6%9e%9a%e8%88%89enums%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e6%9e%9a%e8%88%89enums%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u679a\u8209(Enums)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Enums\"><\/p>\n<h1>TypeScript \u4e2d\u7684\u679a\u8209(Enums)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u6aa2\u67e5\u548c\u975c\u614b\u5206\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\u3002\u5176\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\u5c31\u662f\u679a\u8209(Enums)\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u5e38\u91cf\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u679a\u8209(Enums)<\/h2>\n<p>\u679a\u8209(Enums)\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u985e\u578b\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u6bd4\u5982\u8aaa\uff0c\u4e00\u500b\u679a\u8209\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u984f\u8272\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum Color {\n  Red,\n  Green,\n  Blue\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>Color<\/code> \u7684\u679a\u8209\uff0c\u5b83\u5305\u542b\u4e86\u4e09\u500b\u5e38\u91cf\uff1a<code>Red<\/code>\u3001<code>Green<\/code> \u548c <code>Blue<\/code>\u3002<\/p>\n<p>\u679a\u8209\u7684\u503c\u53ef\u4ee5\u662f\u6578\u5b57\uff0c\u4e5f\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum Color {\n  Red = 1,\n  Green = 2,\n  Blue = 3\n}\n\nenum Color {\n  Red = \"RED\",\n  Green = \"GREEN\",\n  Blue = \"BLUE\"\n}\n<\/pre>\n<p>\u679a\u8209\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7684\u6578\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u5b83\u5011\u5fc5\u9808\u662f\u552f\u4e00\u7684\u3002<\/p>\n<h2>\u679a\u8209\u7684\u512a\u9ede<\/h2>\n<p>\u679a\u8209\u7684\u6700\u5927\u512a\u9ede\u5c31\u662f\u53ef\u4ee5\u6e05\u695a\u5730\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u5b9a\u7fa9\u4e00\u500b\u984f\u8272\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u679a\u8209\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet backgroundColor = Color.Red;\n<\/pre>\n<p>\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053 <code>backgroundColor<\/code> \u7684\u503c\u662f <code>Red<\/code>\uff0c\u800c\u4e0d\u662f\u4e00\u500b\u6578\u5b57\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u679a\u8209\u4e5f\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum Direction {\n  Up,\n  Down,\n  Left,\n  Right\n}\n\nlet goLeft = Direction.Left;\nlet goRight = Direction.Right;\n<\/pre>\n<p>\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053 <code>goLeft<\/code> \u548c <code>goRight<\/code> \u7684\u503c\u5206\u5225\u662f <code>Left<\/code> \u548c <code>Right<\/code>\uff0c\u800c\u4e0d\u662f\u4e00\u500b\u6578\u5b57\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u679a\u8209(Enums)\u662f TypeScript \u4e2d\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u5e38\u91cf\u3002\u5b83\u7684\u512a\u9ede\u662f\u53ef\u4ee5\u6e05\u695a\u5730\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\uff0c\u4e26\u4e14\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u91cf\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u679a\u8209\u7684\u4f7f\u7528<\/h2>\n<p>\u679a\u8209\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum Direction {\n  Up,\n  Down,\n  Left,\n  Right\n}\n\nlet goLeft = Direction.Left;\nlet goRight = Direction.Right;\n<\/pre>\n<p>\u679a\u8209\u4e5f\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u6578\u5b57\u6216\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum Color {\n  Red = 1,\n  Green = 2,\n  Blue = 3\n}\n\nenum Color {\n  Red = \"RED\",\n  Green = \"GREEN\",\n  Blue = \"BLUE\"\n}\n<\/pre>\n<p>\u679a\u8209\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7684\u6578\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u5b83\u5011\u5fc5\u9808\u662f\u552f\u4e00\u7684\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u679a\u8209(Enums)\u662f TypeScript \u4e2d\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u5e38\u91cf\u3002\u5b83\u7684\u512a\u9ede\u662f\u53ef\u4ee5\u6e05\u695a\u5730\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\uff0c\u4e26\u4e14\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u91cf\u3002\u679a\u8209\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u5e38\u91cf\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u6578\u5b57\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):TypeScript\u4e2d\u7684\u679a\u8209(Enums)\u662f\u4e00\u7a2e\u53ef\u4ee5\u5c07\u6578\u503c\u8f49\u63db\u70ba\u53ef\u8b80\u6027\u66f4\u9ad8\u7684\u5b57\u4e32\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u679a\u8209(Enums)\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-4864","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1gs","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4864"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4864\/revisions"}],"predecessor-version":[{"id":4865,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4864\/revisions\/4865"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}