{"id":5052,"date":"2023-01-05T11:46:02","date_gmt":"2023-01-05T03:46:02","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5052"},"modified":"2023-01-05T11:46:02","modified_gmt":"2023-01-05T03:46:02","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e6%a0%bc%e5%bc%8f%e5%8c%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e6%a0%bc%e5%bc%8f%e5%8c%96\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u683c\u5f0f\u5316"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript,\u5b57\u4e32\u683c\u5f0f\u5316,format\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316(format)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6027\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u958b\u767c\u51fa\u66f4\u512a\u8cea\u7684\u7a0b\u5f0f\u78bc\u3002\u5176\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u7279\u6027\u5c31\u662f\u5b57\u4e32\u7684\u683c\u5f0f\u5316\uff0c\u4e5f\u5c31\u662f <code>format<\/code>\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316<\/h2>\n<p>TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316\u662f\u4e00\u7a2e\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c0d\u5b57\u4e32\u9032\u884c\u66f4\u8907\u96dc\u7684\u683c\u5f0f\u5316\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>format<\/code> \u4f86\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u4fbf\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u5176\u4ed6\u5b57\u4e32\uff0c\u6216\u8005\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u7279\u5b9a\u7684\u503c\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316<\/h2>\n<p>\u4f7f\u7528 TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u4f7f\u7528 <code>String.prototype.format<\/code> \u65b9\u6cd5\u5373\u53ef\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u5176\u4ed6\u5b57\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str = 'Hello {0}';\nlet result = str.format('World');\nconsole.log(result); \/\/ Hello World\n<\/pre>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u8981\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str = 'The answer is {0}';\nlet result = str.format(42);\nconsole.log(result); \/\/ The answer is 42\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f7f\u7528 <code>String.prototype.format<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8f15\u9b06\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u4fbf\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u5176\u4ed6\u5b57\u4e32\uff0c\u6216\u8005\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u7279\u5b9a\u7684\u503c\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u5b57\u4e32\u7684\u683c\u5f0f\u5316\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u7279\u6027\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f7f\u7528 <span style=\"color: #009900; font-weight: bold;\">String.prototype.format<\/span> \u65b9\u6cd5\u53ef\u4ee5\u8f15\u9b06\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u4fbf\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u5176\u4ed6\u5b57\u4e32\uff0c\u6216\u8005\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u66ff\u63db\u70ba\u7279\u5b9a\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u683c\u5f0f\u5316\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3001\u9748\u6d3b\uff0c\u66f4\u5bb9\u6613\u95b1\u8b80\u548c\u7dad\u8b77\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u683c\u5f0f\u5316\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u9748\u6d3b\uff0c\u66f4\u5bb9\u6613\u95b1\u8b80\u548c\u7dad\u8b77\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-5052","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-1ju","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5052","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=5052"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5052\/revisions"}],"predecessor-version":[{"id":5053,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5052\/revisions\/5053"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}