{"id":5416,"date":"2023-01-05T12:38:08","date_gmt":"2023-01-05T04:38:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5416"},"modified":"2023-01-05T12:38:08","modified_gmt":"2023-01-05T04:38:08","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84enums%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%84enums%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684Enums\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, enums\"><\/p>\n<h1>\u4f7f\u7528 TypeScript Enums \u4f86\u589e\u52a0\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u985e\u578b\u6aa2\u67e5\u548c\u975c\u614b\u5206\u6790\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u958b\u767c\u51fa\u53ef\u9760\u7684\u7a0b\u5f0f\u78bc\u3002\u5176\u4e2d\u4e00\u500b\u529f\u80fd\u5c31\u662f Enums\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u6578\u5b9a\u7fa9\u5728\u4e00\u8d77\uff0c\u4ee5\u589e\u52a0\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>Enums \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u6578\u5b9a\u7fa9\u5728\u4e00\u8d77\uff0c\u4ee5\u589e\u52a0\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u61c9\u7528\u7a0b\u5f0f\uff0c\u5b83\u9700\u8981\u8655\u7406\u4e0d\u540c\u7684\u985e\u578b\u7684\u7528\u6236\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 Enums \u4f86\u5b9a\u7fa9\u4e0d\u540c\u7684\u7528\u6236\u985e\u578b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nenum UserType {\n  Admin,\n  User,\n  Guest\n}\n<\/pre>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4f7f\u7528\u9019\u4e9b Enums \u4f86\u6aa2\u67e5\u7528\u6236\u7684\u985e\u578b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nif (user.type === UserType.Admin) {\n  \/\/ Do something\n}\n<\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053\u6211\u5011\u6b63\u5728\u6aa2\u67e5\u7684\u662f\u4ec0\u9ebc\u985e\u578b\u7684\u7528\u6236\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u6578\u5b57\u6216\u5b57\u4e32\u4f86\u8868\u793a\u3002<\/p>\n<p>Enums \u9084\u53ef\u4ee5\u7528\u65bc\u5b9a\u7fa9\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u6578\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\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>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4f7f\u7528\u9019\u4e9b Enums \u4f86\u8a2d\u5b9a\u5143\u7d20\u7684\u984f\u8272\uff1a<\/p>\n<pre class=\"brush: typescript\">\nelement.style.backgroundColor = Color.Red;\n<\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053\u6211\u5011\u6b63\u5728\u8a2d\u5b9a\u7684\u662f\u4ec0\u9ebc\u984f\u8272\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u6578\u5b57\u6216\u5b57\u4e32\u4f86\u8868\u793a\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u7684 Enums \u529f\u80fd\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u7d44\u76f8\u95dc\u7684\u5e38\u6578\u5b9a\u7fa9\u5728\u4e00\u8d77\uff0c\u4ee5\u589e\u52a0\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u4e26\u6e1b\u5c11\u51fa\u932f\u7684\u6a5f\u6703\u3002\u4f7f\u7528 Enums \u4e5f\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u5bb9\u6613\u95b1\u8b80\u548c\u7dad\u8b77\uff0c\u56e0\u6b64\u5efa\u8b70\u958b\u767c\u8005\u5728\u958b\u767c TypeScript \u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u76e1\u53ef\u80fd\u591a\u5730\u4f7f\u7528 Enums \u4f86\u589e\u52a0\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3TypeScript\u4e2d\u7684Enums\u529f\u80fd\uff0cEnums\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\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39Enums\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728TypeScript\u4e2d\u4f7f\u7528Enums\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-5416","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-1pm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5416","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=5416"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5416\/revisions"}],"predecessor-version":[{"id":5417,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5416\/revisions\/5417"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}