{"id":4952,"date":"2023-01-05T11:26:06","date_gmt":"2023-01-05T03:26:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4952"},"modified":"2023-01-05T11:26:06","modified_gmt":"2023-01-05T03:26:06","slug":"%e5%88%a9%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e6%b1%82%e5%96%ae%e8%a9%9e%e8%a8%88%e6%95%b8%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%88%a9%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e6%b1%82%e5%96%ae%e8%a9%9e%e8%a8%88%e6%95%b8%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u5229\u7528TypeScript\u5b57\u4e32\u6c42\u55ae\u8a5e\u8a08\u6578\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u5b57\u4e32, \u6c42\u55ae\u8a5e\u8a08\u6578, String Word Count\"><\/p>\n<h1>TypeScript \u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u7684\u6240\u6709\u529f\u80fd\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u984d\u5916\u7684\u7279\u6027\uff0c\u4f8b\u5982\u985e\u578b\u6aa2\u67e5\u548c\u975c\u614b\u5206\u6790\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)\u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\u3002<\/p>\n<p>\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)\u662f\u4e00\u7a2e\u7c21\u55ae\u7684\u6f14\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\u3002\u5b83\u7684\u539f\u7406\u662f\u5c07\u5b57\u4e32\u4e2d\u7684\u6bcf\u500b\u55ae\u8a5e\u90fd\u8a08\u7b97\u51fa\u4f86\uff0c\u7136\u5f8c\u5c07\u5b83\u5011\u76f8\u52a0\uff0c\u5f97\u5230\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\u3002<\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u5be6\u73fe\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction wordCount(str: string): number {\n  let words = str.split(' ');\n  return words.length;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 TypeScript \u7684 split() \u65b9\u6cd5\u5c07\u5b57\u4e32\u62c6\u5206\u6210\u55ae\u8a5e\uff0c\u7136\u5f8c\u4f7f\u7528 length \u5c6c\u6027\u4f86\u8a08\u7b97\u55ae\u8a5e\u7684\u6578\u91cf\u3002<\/p>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u6e2c\u8a66\u6211\u5011\u7684\u51fd\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str = 'Hello World!';\nlet wordCount = wordCount(str);\nconsole.log(wordCount); \/\/ 2\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u4e32 &#8220;Hello World!&#8221; \u50b3\u905e\u7d66 wordCount() \u51fd\u6578\uff0c\u7136\u5f8c\u5c07\u8fd4\u56de\u503c\u8ce6\u503c\u7d66 wordCount \u8b8a\u6578\uff0c\u6700\u5f8c\u4f7f\u7528 console.log() \u65b9\u6cd5\u5c07 wordCount \u7684\u503c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7d50\u679c\u70ba 2\uff0c\u8868\u793a\u5b57\u4e32 &#8220;Hello World!&#8221; \u4e2d\u6709 2 \u500b\u55ae\u8a5e\u3002<\/p>\n<p>\u7e3d\u7d50\uff0c\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)\u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\uff0c\u9019\u662f\u4e00\u7a2e\u7c21\u55ae\u7684\u6f14\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 TypeScript \u5be6\u73fe\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)<\/h2>\n<p>\u8981\u4f7f\u7528 TypeScript \u5be6\u73fe\u5b57\u4e32\u7684\u6c42\u55ae\u8a5e\u8a08\u6578(String Word Count)\uff0c\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u51fd\u6578\u7684\u53c3\u6578\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\u3002<\/p>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 TypeScript \u7684 split() \u65b9\u6cd5\u5c07\u5b57\u4e32\u62c6\u5206\u6210\u55ae\u8a5e\uff0c\u7136\u5f8c\u4f7f\u7528 length \u5c6c\u6027\u4f86\u8a08\u7b97\u55ae\u8a5e\u7684\u6578\u91cf\uff0c\u6700\u5f8c\u5c07\u8a08\u7b97\u51fa\u4f86\u7684\u55ae\u8a5e\u6578\u91cf\u4f5c\u70ba\u51fd\u6578\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u6e2c\u8a66\u6211\u5011\u7684\u51fd\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str = 'Hello World!';\nlet wordCount = wordCount(str);\nconsole.log(wordCount); \/\/ 2\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u4e32 &#8220;Hello World!&#8221; \u50b3\u905e\u7d66 wordCount() \u51fd\u6578\uff0c\u7136\u5f8c\u5c07\u8fd4\u56de\u503c\u8ce6\u503c\u7d66 wordCount \u8b8a\u6578\uff0c\u6700\u5f8c\u4f7f\u7528 console.log() \u65b9\u6cd5\u5c07 wordCount \u7684\u503c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7d50\u679c\u70ba 2\uff0c\u8868\u793a\u5b57\u4e32 &#8220;Hello World!&#8221; \u4e2d\u6709 2 \u500b\u55ae\u8a5e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u6c42\u55ae\u8a5e\u8a08\u6578\u7684\u65b9\u6cd5\uff0c\u8a73\u7d30\u89e3\u91cb\u4e86\u5982\u4f55\u4f7f\u7528methods()\u51fd\u6578\u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u7684\u55ae\u8a5e\u6578\u91cf\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u4f86\u8b93\u8b80\u8005\u66f4\u5bb9\u6613\u7406\u89e3\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-4952","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-1hS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4952","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=4952"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4952\/revisions"}],"predecessor-version":[{"id":4953,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4952\/revisions\/4953"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}