{"id":4816,"date":"2023-01-05T10:32:12","date_gmt":"2023-01-05T02:32:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4816"},"modified":"2023-01-05T10:32:12","modified_gmt":"2023-01-05T02:32:12","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%97%ad%e5%8c%85closure%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%e9%97%ad%e5%8c%85closure%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u95ed\u5305(Closure)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u95ed\u5305, Closure\"><\/p>\n<h1>TypeScript \u95ed\u5305(Closure) \u4ecb\u7d39<\/h1>\n<p>TypeScript \u95ed\u5305\uff08Closure\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u5171\u4eab\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/p>\n<p>TypeScript \u95ed\u5305\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u8fd9\u610f\u5473\u7740\uff0c\u95ed\u5305\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 TypeScript \u95ed\u5305\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction outerFunction(arg) {\n  let variableInOuterFunction = arg;\n\n  function innerFunction() {\n    console.log(variableInOuterFunction);\n  }\n  innerFunction();\n}\nouterFunction(\"Hello Closure!\");\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5916\u90e8\u51fd\u6570 outerFunction\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 arg\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5185\u90e8\u51fd\u6570 innerFunction\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf variableInOuterFunction\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u8c03\u7528 outerFunction \u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u5e76\u5c06\u53c2\u6570 arg \u7684\u503c\u8d4b\u7ed9\u53d8\u91cf variableInOuterFunction\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u8c03\u7528\u5185\u90e8\u51fd\u6570 innerFunction\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf variableInOuterFunction\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 outerFunction \u65f6\uff0c\u5b83\u4f1a\u6253\u5370\u51fa\u53c2\u6570 arg \u7684\u503c\uff0c\u5373 &#8220;Hello Closure!&#8221;\u3002<\/p>\n<p>TypeScript \u95ed\u5305\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u8fd9\u610f\u5473\u7740\uff0c\u95ed\u5305\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684 TypeScript \u95ed\u5305\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction outerFunction(arg) {\n  let variableInOuterFunction = arg;\n\n  return function innerFunction() {\n    console.log(variableInOuterFunction);\n  }\n}\nlet innerFunction = outerFunction(\"Hello Closure!\");\ninnerFunction();\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5916\u90e8\u51fd\u6570 outerFunction\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 arg\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5185\u90e8\u51fd\u6570 innerFunction\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf variableInOuterFunction\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u8c03\u7528 outerFunction \u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u5e76\u5c06\u53c2\u6570 arg \u7684\u503c\u8d4b\u7ed9\u53d8\u91cf variableInOuterFunction\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u8fd4\u56de\u5185\u90e8\u51fd\u6570 innerFunction\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf variableInOuterFunction\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u8c03\u7528 outerFunction \u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd4\u56de\u7684\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf innerFunction\uff0c\u7136\u540e\u8c03\u7528\u5b83\uff0c\u5b83\u4f1a\u6253\u5370\u51fa\u53c2\u6570 arg \u7684\u503c\uff0c\u5373 &#8220;Hello Closure!&#8221;\u3002<\/p>\n<p>\u603b\u4e4b\uff0cTypeScript \u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u5171\u4eab\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5b83\u8fd8\u53ef\u4ee5\u8ba9\u4f60\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/p>\n<p><!--more--><\/p>\n<h2>TypeScript \u95ed\u5305\u7684\u4f18\u70b9<\/h2>\n<p>TypeScript \u95ed\u5305\u6709\u8bb8\u591a\u4f18\u70b9\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u5171\u4eab\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/strong> \u8fd9\u610f\u5473\u7740\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u53d8\u91cf\u540d\uff0c\u800c\u4e0d\u4f1a\u5f15\u8d77\u6df7\u6dc6\u3002<\/li>\n<li><strong>\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/strong> \u8fd9\u610f\u5473\u7740\uff0c\u4f60\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong> \u8fd9\u610f\u5473\u7740\uff0c\u4f60\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7406\u89e3\u4ee3\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u4fee\u6539\u548c\u7ef4\u62a4\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>TypeScript \u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u5171\u4eab\u53d8\u91cf\uff0c\u800c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5b83\u8fd8\u53ef\u4ee5\u8ba9\u4f60\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002TypeScript \u95ed\u5305\u7684\u4f18\u70b9\u5305\u62ec\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u5171\u4eab\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u4ee5\u53ca\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305(Closure)\u662fTypeScript\u4e2d\u4e00\u7a2e\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u51fd\u6570\u5916\u90e8\u8bbf\u95ee\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecdTypeScript\u4e2d\u7684\u95ed\u5305(Closure)\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\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-4816","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-1fG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4816","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=4816"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4816\/revisions"}],"predecessor-version":[{"id":4817,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4816\/revisions\/4817"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}