{"id":4956,"date":"2023-01-05T11:27:15","date_gmt":"2023-01-05T03:27:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4956"},"modified":"2023-01-05T11:27:15","modified_gmt":"2023-01-05T03:27:15","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%b1%82%e9%9a%a8%e6%a9%9f%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%94%9f%e6%88%90","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%b1%82%e9%9a%a8%e6%a9%9f%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%94%9f%e6%88%90\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u6c42\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, String, Random, String Generation\"><\/p>\n<h1>TypeScript \u5b57\u4e32\u7684\u6c42\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210(String Random String Generation)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e\u5f37\u578b\u5225\u7684 JavaScript \u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u958b\u767c\u51fa\u53ef\u7dad\u8b77\u7684\u7a0b\u5f0f\u78bc\u3002\u5728 TypeScript \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u7684\u6c42\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210\uff08String Random String Generation\uff09\u4f86\u7522\u751f\u96a8\u6a5f\u5b57\u4e32\uff0c\u9019\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5b57\u4e32\u7684\u6c42\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210\uff08String Random String Generation\uff09\u53ef\u4ee5\u7528\u4f86\u7522\u751f\u4e00\u500b\u96a8\u6a5f\u7684\u5b57\u4e32\uff0c\u9019\u500b\u5b57\u4e32\u53ef\u4ee5\u7528\u4f86\u505a\u5f88\u591a\u4e8b\u60c5\uff0c\u4f8b\u5982\u7528\u65bc\u5bc6\u78bc\u7684\u52a0\u5bc6\u3001\u7528\u65bc\u5efa\u7acb\u552f\u4e00\u7684ID\u7b49\u7b49\u3002<\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>Math.random()<\/code> \u4f86\u7522\u751f\u4e00\u500b\u96a8\u6a5f\u7684\u6578\u5b57\uff0c\u7136\u5f8c\u518d\u5c07\u9019\u500b\u6578\u5b57\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u5c31\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u96a8\u6a5f\u7684\u5b57\u4e32\u4e86\u3002<\/p>\n<pre class=\"brush: typescript\">\n\/\/ \u7522\u751f\u4e00\u500b 0 \u5230 1 \u4e4b\u9593\u7684\u96a8\u6a5f\u6578\nlet randomNumber = Math.random();\n\n\/\/ \u5c07\u96a8\u6a5f\u6578\u8f49\u63db\u6210\u5b57\u4e32\nlet randomString = randomNumber.toString();\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u96a8\u6a5f\u7684\u5b57\u4e32\uff0c\u4f46\u662f\u9019\u500b\u5b57\u4e32\u53ef\u80fd\u6703\u5f88\u9577\uff0c\u800c\u4e14\u53ef\u80fd\u6703\u6709\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684\u5b57\u5143\uff0c\u6240\u4ee5\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>String.prototype.slice()<\/code> \u4f86\u5c07\u5b57\u4e32\u88c1\u5207\u6210\u6307\u5b9a\u9577\u5ea6\uff0c\u4ee5\u53ca\u53bb\u9664\u4e0d\u5fc5\u8981\u7684\u5b57\u5143\uff1a<\/p>\n<pre class=\"brush: typescript\">\n\/\/ \u5c07\u5b57\u4e32\u88c1\u5207\u6210\u6307\u5b9a\u9577\u5ea6\nlet randomString = randomNumber.toString().slice(0, 10);\n\n\/\/ \u53bb\u9664\u4e0d\u5fc5\u8981\u7684\u5b57\u5143\nlet randomString = randomNumber.toString().replace(\/[^a-zA-Z0-9]\/g, '').slice(0, 10);\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u9577\u5ea6\u70ba 10 \u7684\u96a8\u6a5f\u5b57\u4e32\uff0c\u800c\u4e14\u53ea\u5305\u542b\u82f1\u6587\u5b57\u6bcd\u548c\u6578\u5b57\uff0c\u9019\u6a23\u7684\u5b57\u4e32\u53ef\u4ee5\u7528\u65bc\u5f88\u591a\u5730\u65b9\uff0c\u4f8b\u5982\u7528\u65bc\u5bc6\u78bc\u7684\u52a0\u5bc6\u3001\u7528\u65bc\u5efa\u7acb\u552f\u4e00\u7684ID\u7b49\u7b49\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6b64\u5916\uff0cTypeScript \u9084\u63d0\u4f9b\u4e86\u4e00\u500b <code>crypto.getRandomValues()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u7522\u751f\u4e00\u500b\u5b89\u5168\u7684\u96a8\u6a5f\u5b57\u4e32\uff0c\u9019\u500b\u5b57\u4e32\u53ef\u4ee5\u7528\u65bc\u52a0\u5bc6\u6216\u662f\u5efa\u7acb\u552f\u4e00\u7684ID\uff1a<\/p>\n<pre class=\"brush: typescript\">\n\/\/ \u7522\u751f\u4e00\u500b\u5b89\u5168\u7684\u96a8\u6a5f\u5b57\u4e32\nlet randomString = crypto.getRandomValues(10);\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u7522\u751f\u4e00\u500b\u5b89\u5168\u7684\u96a8\u6a5f\u5b57\u4e32\uff0c\u9019\u500b\u5b57\u4e32\u53ef\u4ee5\u7528\u65bc\u52a0\u5bc6\u6216\u662f\u5efa\u7acb\u552f\u4e00\u7684ID\uff0c\u800c\u4e14\u53ef\u4ee5\u78ba\u4fdd\u5b57\u4e32\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u63d0\u4f9b\u4e86\u4e00\u500b\u975e\u5e38\u65b9\u4fbf\u7684\u5b57\u4e32\u7684\u6c42\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210\uff08String Random String Generation\uff09\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7522\u751f\u96a8\u6a5f\u5b57\u4e32\uff0c\u800c\u4e14\u53ef\u4ee5\u78ba\u4fdd\u5b57\u4e32\u7684\u5b89\u5168\u6027\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\u96a8\u6a5f\u5b57\u7b26\u4e32\u751f\u6210\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u3001\u5b89\u5168\u5730\u7522\u751f\u552f\u4e00\u7684\u5b57\u4e32\uff0c\u4ee5\u6eff\u8db3\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\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-4956","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-1hW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4956","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=4956"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4956\/revisions"}],"predecessor-version":[{"id":4957,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4956\/revisions\/4957"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}