{"id":17234,"date":"2025-05-08T22:29:34","date_gmt":"2025-05-08T14:29:34","guid":{"rendered":"https:\/\/92it.top\/?p=17234"},"modified":"2025-05-08T22:32:37","modified_gmt":"2025-05-08T14:32:37","slug":"javascript-%e9%97%ad%e5%8c%85%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17234","title":{"rendered":"JavaScript \u95ed\u5305\u89e3\u91ca\uff08\u63a8\u8350\uff09"},"content":{"rendered":"\n<p><strong>\u95ed\u5305\u5230\u5e95\u662f\u4ec0\u4e48<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4eba\u4eba\u90fd\u5728\u8c08\u8bba\u95ed\u5305\uff08closure\uff09\uff0c\u8ba4\u4e3a\u5b83\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u8bed\u8a00\u7279\u6027\uff0c\u4f46\u6709\u591a\u5c11\u4eba\u771f\u6b63\u660e\u767d\u95ed\u5305\u662f\u4ec0\u4e48\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u5462\uff1f\u5c11\u4e4b\u53c8\u5c11\u3002\u95ed\u5305\u662f\u4eba\u4eba\u90fd\u60f3\u7406\u89e3\u7684\u8bed\u8a00\u7279\u6027\uff0c\u4e5f\u662f\u6240\u6709\u4f20\u7edf\u8bed\u8a00\u90fd\u60f3\u6dfb\u52a0\u7684\u8bed\u8a00\u7279\u6027\u3002\u95ed\u5305\u5f88\u96be\u5b66\uff0c\u4e1a\u754c\u5f88\u591a\u53d7\u8fc7\u826f\u597d\u6559\u80b2\u7684\u4eba\u90fd\u8fd9\u6837\u8bf4\uff1b\u4f46\u5bf9\u672c\u4e66\u7684\u8bfb\u8005\u6765\u8bf4\uff0c\u8fd9\u6839\u672c\u5c31\u4e0d\u662f\u95ee\u9898\u3002\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48\uff1f\u4e0d\u662f\u56e0\u4e3a\u672c\u4e66\u901a\u4fd7\u6613\u61c2\uff0c\u4e5f\u4e0d\u662f\u56e0\u4e3a\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2a\u6740\u624b\u7ea7\u5e94\u7528\u7a0b\u5e8f\u6765\u5411\u4f60\u4ecb\u7ecd\u95ed\u5305\uff0c\u800c\u662f\u56e0\u4e3a\u4f60\u65e9\u5c31\u5b66\u4e60\u8fc7\u4e86\uff0c\u53ea\u662f\u90a3\u65f6\u6211\u4eec\u6ca1\u6709\u79f0\u4e4b\u4e3a\u95ed\u5305\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u95f2\u8bdd\u5c11\u8bf4\uff0c\u7ed9\u95ed\u5305\u4e00\u4e2a\u975e\u5e38\u6b63\u5f0f\u7684\u5b9a\u4e49\u5427\u3002<\/p>\n\n\n\n<p><strong>\u95ed\u5305<\/strong>\uff1a\u540d\u8bcd\uff0c\u6307\u7684\u662f\u51fd\u6570\u548c\u5f15\u7528\u73af\u5883\u3002<\/p>\n\n\n\n<p><strong>\u5b9a\u4e49\u5c42\u9762<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u51fd\u6570\u901a\u5e38\u5305\u542b\u5c40\u90e8\u53d8\u91cf\uff08\u5b83\u4eec\u662f\u5728\u51fd\u6570\u4f53\u4e2d\u5b9a\u4e49\u7684\uff0c\u5305\u62ec\u6240\u6709\u7684\u5f62\u53c2\uff09\uff0c\u8fd8\u53ef\u80fd\u5305\u542b\u4e0d\u662f\u5728\u672c\u5730\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u88ab\u79f0\u4e3a<strong>\u81ea\u7531\u53d8\u91cf<\/strong>\u3002\u81ea\u7531\u4e00\u8bcd\u6e90\u4e8e\u8fd9\u6837\u4e00\u70b9\uff1a\u5728\u51fd\u6570\u4f53\u5185\uff0c\u81ea\u7531\u53d8\u91cf\u6ca1\u6709\u7ed1\u5b9a\u5230\u4efb\u4f55\u503c\uff08\u6362\u800c\u8a00\u4e4b\uff0c\u5b83\u4eec\u4e0d\u662f\u5728\u672c\u5730\u58f0\u660e\u7684\uff09\u3002\u6709\u4e86\u7ed9\u6bcf\u4e2a\u81ea\u7531\u53d8\u91cf\u90fd\u63d0\u4f9b\u4e86\u503c\u7684\u73af\u5883\u540e\uff0c\u4fbf\u5c06\u51fd\u6570\u6572\u5b9a\u4e86\uff1b\u800c\u51fd\u6570\u548c\u73af\u5883\u4e00\u8d77\u88ab\u79f0\u4e3a\u95ed\u5305\u3002<\/p>\n\n\n\n<p>\u4ece\u5b9a\u4e49\u6765\u8bb2\uff0c\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u5b9a\u4e49\u540e\uff0c\u5176\u5185\u90e8\u53ef\u80fd\u4f1a\u5f15\u7528\u4e00\u4e9b\u53d8\u91cf\u3002\u5982\u679c\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u662f\u901a\u8fc7\u51fd\u6570\u7684\u53c2\u6570\u4f20\u5165\uff0c\u4e5f\u4e0d\u662f\u5728\u51fd\u6570\u5185\u90e8\u7528<code>let<\/code>\u3001<code>const<\/code>\u3001<code>var<\/code>\u7b49\u5173\u952e\u5b57\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u5b83\u4eec\u5c31\u662f\u81ea\u7531\u53d8\u91cf\u3002\u4f8b\u5982<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let a = 10; \/\/ \u5c40\u90e8\u53d8\u91cf\n    function inner() {\n        console.log(a); \/\/ a \u65e2\u4e0d\u662f inner \u51fd\u6570\u7684\u53c2\u6570\uff0c\u4e5f\u4e0d\u662f inner \u51fd\u6570\u5185\u90e8\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5 a \u662f\u81ea\u7531\u53d8\u91cf\n    }\n    return inner;\n}\nlet fn = outer();\nfn(); <\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>inner<\/code>\u51fd\u6570\u5185\u5f15\u7528\u7684<code>a<\/code>\u53d8\u91cf\uff0c\u5c31\u662f\u81ea\u7531\u53d8\u91cf\u3002<code>outer<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u540e\uff0c\u6309\u5e38\u7406\u5176\u5c40\u90e8\u53d8\u91cf<code>a<\/code>\u7684\u751f\u547d\u5468\u671f\u5e94\u8be5\u7ed3\u675f\uff0c\u4f46\u7531\u4e8e<code>inner<\/code>\u51fd\u6570\u5f62\u6210\u4e86\u95ed\u5305\uff0c\u4f7f\u5f97<code>a<\/code>\u88ab<code>inner<\/code>\u51fd\u6570\u5f15\u7528\uff0c\u4ece\u800c\u7ee7\u7eed\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u4f9b<code>inner<\/code>\u51fd\u6570\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u5b9a\u4e49<\/strong>: \u5728 JavaScript \u4e2d\uff0c\u95ed\u5305\u6307\u7684\u662f\u6709\u6743\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u91cc\u53d8\u91cf\u7684\u51fd\u6570\u3002\u5f62\u6210\u95ed\u5305\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5b58\u5728\u51fd\u6570\u5d4c\u5957\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u3002<\/li>\n\n\n\n<li>\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u5916\u90e8\u51fd\u6570\u5c06\u5185\u90e8\u51fd\u6570\u8fd4\u56de\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\u795e\u5947\u7684\u8ba1\u6570\u5668<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6709\u8fc7\u5b9e\u73b0\u8ba1\u6570\u5668\u51fd\u6570\u7684\u5ff5\u5934\u5417\uff1f\u901a\u5e38\u4ee5\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var count = 0;\n\/\/ \u6bcf\u6b21\u8c03\u7528counter\u65f6\uff0c\u90fd\u5c06\u5168\u5c40\u53d8\u91cf\n\/\/ count\u52a01\uff0c\u518d\u8fd4\u56de\u7ed3\u679c\u3002\nfunction counter() {\n  count = count + 1;\n  return count;\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\u8fd9\u4e2acounter\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">console.log(counter());\nconsole.log(counter());\nconsole.log(counter());<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"266\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247.png\" alt=\"\" class=\"wp-image-17236\" style=\"width:246px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247.png 432w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-300x185.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-350x216.png 350w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/figure><\/div>\n\n\n<p>\u8fd9\u79cd\u505a\u6cd5\u5b58\u5728\u7684\u552f\u4e00\u95ee\u9898\u662f\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cfcount\uff0c\u534f\u4f5c\u5f00\u53d1\u4ee3\u7801\u65f6\uff0c\u5927\u5bb6\u5e38\u5e38\u4f1a\u4f7f\u7528\u76f8\u540c\u7684\u53d8\u91cf\u540d\uff0c\u8fdb\u800c\u5bfc\u81f4\u51b2\u7a81\u3002\u8fd9\u91cc\u8981\u544a\u8bc9\u4f60\u7684\u662f\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d7\u4fdd\u62a4\u7684\u5c40\u90e8\u53d8\u91cf\u5b9e\u73b0\u8ba1\u6570\u5668\u3002\u8fd9\u6837\uff0c\u8ba1\u6570\u5668\u5c06\u4e0d\u4f1a\u4e0e\u4efb\u4f55\u4ee3\u7801\u53d1\u751f\u51b2\u7a81\uff0c\u4e14\u53ea\u80fd\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\uff08\u4e5f\u53eb\u95ed\u5305\uff09\u6765\u589e\u52a0\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u8981\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\u8fd9\u79cd\u8ba1\u6570\u5668\uff0c\u53ef\u91cd\u7528\u524d\u9762\u7684\u5927\u90e8\u5206\u4ee3\u7801\u3002\u522b\u7728\u773c\uff0c\u5947\u8ff9\u5c31\u8981\u53d1\u751f\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function makeCounter() {\n\/\/ \u6211\u4eec\u5728\u51fd\u6570makeCounter\u4e2d\u58f0\u660e\u53d8\u91cfcount\u3002\u8fd9\u6837\u5b83\u5c31\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\u3002\nvar count = 0;\n  \/\/ \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u51fd\u6570 counter\uff0c\u5b83\u5c06\u53d8\u91cfcount\u52a01\u3002\n  function counter() {\n    count = count + 1;\n    return count;\n  }\n  \/\/ \u7136\u540e\uff0c\u8fd4\u56de\u51fd\u6570counter\u3002\u8fd9\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5728\u5176\u73af\u5883\u4e2d\u5b58\u50a8\u4e86\u53d8\u91cfcount\u3002\n return counter;\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var doCount = makeCounter();\nconsole.log(doCount());\nconsole.log(doCount());\nconsole.log(doCount());<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"438\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1.png\" alt=\"\" class=\"wp-image-17237\" style=\"width:396px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1.png 684w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1-300x192.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1-230x147.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1-350x224.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-1-480x307.png 480w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"714\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-1024x714.png\" alt=\"\" class=\"wp-image-17239\" style=\"width:786px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-1024x714.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-300x209.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-768x536.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-1536x1072.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-830x579.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-230x160.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-350x244.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3-480x335.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/05\/\u56fe\u7247-3.png 1852w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u95ed\u5305\u7684\u4f18\u70b9\u662f\u4ec0\u4e48<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>JavaScript \u95ed\u5305\u5177\u6709\u8bb8\u591a\u4f18\u70b9\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\uff0c\u4e0b\u9762\u4e3a\u4f60\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n\n\n\n<p><strong>\u8bfb\u53d6\u51fd\u6570\u5185\u90e8\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>\u5728 JavaScript \u91cc\uff0c\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u5177\u6709\u5c40\u90e8\u4f5c\u7528\u57df\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u3002\u4f46\u95ed\u5305\u80fd\u591f\u7a81\u7834\u8fd9\u4e00\u9650\u5236\uff0c\u8ba9\u6211\u4eec\u5728\u51fd\u6570\u5916\u90e8\u8bfb\u53d6\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let innerVariable = '\u6211\u662f\u5185\u90e8\u53d8\u91cf';\n    return function() {\n        return innerVariable;\n    };\n}\nlet getInnerVariable = outer();\nconsole.log(getInnerVariable()); <\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>outer<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u5f62\u6210\u4e86\u95ed\u5305\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728<code>outer<\/code>\u51fd\u6570\u5916\u90e8\u901a\u8fc7\u8c03\u7528<code>getInnerVariable<\/code>\u51fd\u6570\u6765\u83b7\u53d6<code>outer<\/code>\u51fd\u6570\u5185\u90e8\u7684<code>innerVariable<\/code>\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u8ba9\u53d8\u91cf\u7684\u503c\u59cb\u7ec8\u4fdd\u6301\u5728\u5185\u5b58\u4e2d<\/strong><\/p>\n\n\n\n<p>\u95ed\u5305\u80fd\u591f\u8ba9\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u59cb\u7ec8\u4fdd\u6301\u5728\u5185\u5b58\u4e2d\uff0c\u4e0d\u4f1a\u5728\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u9500\u6bc1\u3002\u8fd9\u4e00\u7279\u6027\u4f7f\u5f97\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u4e00\u4e9b\u9700\u8981\u4fdd\u5b58\u72b6\u6001\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function createCounter() {\n    let count = 0;\n    return function() {\n        return ++count;\n    };\n}\nlet counter = createCounter();\nconsole.log(counter()); \nconsole.log(counter()); <\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>createCounter<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\u6bcf\u6b21\u8c03\u7528<code>counter<\/code>\u51fd\u6570\u65f6\uff0c\u90fd\u4f1a\u5bf9<code>count<\/code>\u53d8\u91cf\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\u3002\u7531\u4e8e\u95ed\u5305\u7684\u5b58\u5728\uff0c<code>count<\/code>\u53d8\u91cf\u7684\u503c\u4f1a\u4e00\u76f4\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4e0d\u4f1a\u5728<code>createCounter<\/code>\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u9500\u6bc1\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u73b0\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf<\/strong><\/p>\n\n\n\n<p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf\uff0c\u5c06\u4e00\u4e9b\u6570\u636e\u548c\u64cd\u4f5c\u5c01\u88c5\u5728\u95ed\u5305\u5185\u90e8\uff0c\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u7ed9\u5916\u90e8\u4f7f\u7528\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function createPerson(name) {\n    let privateName = name;\n    return {\n        getName: function() {\n            return privateName;\n        },\n        setName: function(newName) {\n            privateName = newName;\n        }\n    };\n}\nlet person = createPerson('John');\nconsole.log(person.getName()); \nperson.setName('Jane');\nconsole.log(person.getName()); \n\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>createPerson<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u4e24\u4e2a\u65b9\u6cd5\uff1a<code>getName<\/code>\u548c<code>setName<\/code>\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5f62\u6210\u4e86\u95ed\u5305\uff0c\u5b83\u4eec\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539<code>createPerson<\/code>\u51fd\u6570\u5185\u90e8\u7684<code>privateName<\/code>\u53d8\u91cf\u3002\u5916\u90e8\u4ee3\u7801\u53ea\u80fd\u901a\u8fc7\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u6765\u64cd\u4f5c<code>privateName<\/code>\u53d8\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u6570\u636e\u7684\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf\u3002<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u673a\u5236\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u95ed\u5305\u4e0d\u88ab\u56de\u6536\u4e3b\u8981\u4e0e JavaScript \u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u3001\u4f5c\u7528\u57df\u94fe\u548c\u5f15\u7528\u5173\u7cfb\u6709\u5173\u3002\u4e0b\u9762\u4e3a\u4f60\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n\n\n\n<p><strong>JavaScript \u5185\u5b58\u7ba1\u7406\u673a\u5236<\/strong><\/p>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u5185\u5b58\u7ba1\u7406\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff0c\u7531\u5783\u573e\u56de\u6536\u673a\u5236\uff08Garbage Collection\uff0cGC\uff09\u8d1f\u8d23\u3002\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u6838\u5fc3\u601d\u60f3\u662f\u901a\u8fc7\u6807\u8bb0\u548c\u6e05\u9664\u7b49\u7b97\u6cd5\uff0c\u627e\u51fa\u90a3\u4e9b\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u5e76\u91ca\u653e\u5b83\u4eec\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u6ca1\u6709\u4efb\u4f55\u5f15\u7528\u6307\u5411\u5b83\uff0c\u90a3\u4e48\u5b83\u5c31\u4f1a\u88ab\u89c6\u4e3a \u201c\u5783\u573e\u201d\uff0c\u5e76\u5728\u5408\u9002\u7684\u65f6\u673a\u88ab\u56de\u6536\u3002<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u4f5c\u7528\u57df\u94fe\u548c\u5f15\u7528\u5173\u7cfb<\/strong><\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e86\u53e6\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u5185\u90e8\u51fd\u6570\u4f1a\u5f62\u6210\u4e00\u4e2a\u95ed\u5305\u3002\u95ed\u5305\u4f1a\u6355\u83b7\u5e76\u4fdd\u7559\u5176\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u5176\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>\u4f5c\u7528\u57df\u94fe\u7684\u4fdd\u7559<\/strong><\/p>\n\n\n\n<p>\u51fd\u6570\u5728\u521b\u5efa\u65f6\u4f1a\u4fdd\u5b58\u5176\u6240\u5728\u7684\u4f5c\u7528\u57df\u94fe\u3002\u5bf9\u4e8e\u95ed\u5305\u800c\u8a00\uff0c\u5185\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u5305\u542b\u4e86\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u3002\u8fd9\u610f\u5473\u7740\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u548c\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let outerVariable = 'I am from outer function';\n    function inner() {\n        console.log(outerVariable);\n    }\n    return inner;\n}\n\nlet closure = outer();\n\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u5f62\u6210\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\u5f53\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u4ecd\u7136\u4fdd\u7559\u7740\u5bf9\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\u3002\u5373\u4f7f\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\uff0c\u5176\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u00a0<code>outerVariable<\/code>\u00a0\u4e5f\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u56e0\u4e3a\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u6307\u5411\u4e86\u5b83\u3002<\/p>\n\n\n\n<p><strong>\u5f15\u7528\u5173\u7cfb\u7684\u5b58\u5728<\/strong><\/p>\n\n\n\n<p>\u53ea\u8981\u95ed\u5305\u5b58\u5728\uff0c\u5b83\u5c31\u4f1a\u4fdd\u6301\u5bf9\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u5185\u53d8\u91cf\u7684\u5f15\u7528\u3002\u53ea\u8981\u6709\u5f15\u7528\u5b58\u5728\uff0c\u5783\u573e\u56de\u6536\u673a\u5236\u5c31\u4e0d\u4f1a\u5c06\u8fd9\u4e9b\u53d8\u91cf\u89c6\u4e3a\u53ef\u56de\u6536\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf&nbsp;<code>closure<\/code>&nbsp;\u5f15\u7528\u4e86&nbsp;<code>inner<\/code>&nbsp;\u51fd\u6570\uff0c\u800c&nbsp;<code>inner<\/code>&nbsp;\u51fd\u6570\u53c8\u5f15\u7528\u4e86&nbsp;<code>outerVariable<\/code>\u3002\u56e0\u6b64\uff0c<code>outerVariable<\/code>&nbsp;\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u76f4\u5230&nbsp;<code>closure<\/code>&nbsp;\u4e0d\u518d\u5f15\u7528&nbsp;<code>inner<\/code>&nbsp;\u51fd\u6570\uff0c\u5e76\u4e14\u6ca1\u6709\u5176\u4ed6\u5f15\u7528\u6307\u5411&nbsp;<code>outerVariable<\/code>&nbsp;\u4e3a\u6b62\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\u8bf4\u660e\u95ed\u5305\u4e0d\u88ab\u56de\u6536\u7684\u5f71\u54cd<\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u95ed\u5305\u5982\u4f55\u5f71\u54cd\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function createCounter() {\n    let count = 0;\n    return function() {\n        return ++count;\n    };\n}\n\nlet counter = createCounter();\nconsole.log(counter()); \nconsole.log(counter()); <\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>createCounter<\/code>&nbsp;\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\u6bcf\u6b21\u8c03\u7528&nbsp;<code>counter<\/code>&nbsp;\u51fd\u6570\u65f6\uff0c\u90fd\u4f1a\u5bf9&nbsp;<code>count<\/code>&nbsp;\u53d8\u91cf\u8fdb\u884c\u81ea\u589e\u64cd\u4f5c\u3002\u7531\u4e8e\u95ed\u5305\u7684\u5b58\u5728\uff0c<code>count<\/code>&nbsp;\u53d8\u91cf\u4f1a\u4e00\u76f4\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4e0d\u4f1a\u5728&nbsp;<code>createCounter<\/code>&nbsp;\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u9500\u6bc1\u3002\u53ea\u8981&nbsp;<code>counter<\/code>&nbsp;\u53d8\u91cf\u4ecd\u7136\u5f15\u7528\u7740\u8fd9\u4e2a\u95ed\u5305\uff0c<code>count<\/code>&nbsp;\u53d8\u91cf\u5c31\u4f1a\u4e00\u76f4\u5b58\u5728\u3002<\/p>\n\n\n\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u95ed\u5305\u4e0d\u88ab\u56de\u6536\u662f\u56e0\u4e3a\u5b83\u4fdd\u7559\u4e86\u5bf9\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\uff0c\u4f7f\u5f97\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u4e0d\u4f1a\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u89c6\u4e3a\u53ef\u56de\u6536\u7684\u5bf9\u8c61\u3002\u8fd9\u79cd\u7279\u6027\u5728\u5b9e\u73b0\u4e00\u4e9b\u529f\u80fd\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u4e0d\u6ce8\u610f\uff0c\u4e5f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<p><strong>\u8fd9\u4e2a\u662f\u95ed\u5305\u5417\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let outerVariable = 'I am from outer function';\n    function inner() {\n        console.log(\u201chello\u201d);\n    }\n    return inner;\n}\n\u8fd9\u6837\u7684\u8bdd inner\u7b97\u662f\u95ed\u5305\u5417<\/pre>\n\n\n\n<p>\u4e25\u683c\u6765\u8bf4\uff0c\u5728\u4f60\u7ed9\u51fa\u7684\u4ee3\u7801\u91cc<code>inner<\/code>\u4e0d\u7b97\u95ed\u5305\u3002\u63a5\u4e0b\u6765\u4e3a\u4f60\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n\n\n\n<p><strong>\u95ed\u5305\u7684\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>\u5728 JavaScript \u4e2d\uff0c\u95ed\u5305\u6307\u7684\u662f\u6709\u6743\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u91cc\u53d8\u91cf\u7684\u51fd\u6570\u3002\u5f62\u6210\u95ed\u5305\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5b58\u5728\u51fd\u6570\u5d4c\u5957\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u5b9a\u4e49\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u3002<\/li>\n\n\n\n<li>\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u5916\u90e8\u51fd\u6570\u5c06\u5185\u90e8\u51fd\u6570\u8fd4\u56de\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5bf9\u4ee3\u7801\u7684\u5206\u6790<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5b58\u5728\u51fd\u6570\u5d4c\u5957\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u5b9a\u4e49\u5728\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u5185\u90e8\u3002<\/li>\n\n\n\n<li>\u5916\u90e8\u51fd\u6570\u00a0<code>outer<\/code>\u00a0\u5c06\u5185\u90e8\u51fd\u6570\u00a0<code>inner<\/code>\u00a0\u8fd4\u56de\u3002<\/li>\n\n\n\n<li>\u7136\u800c\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u5185\u90e8\u5e76\u6ca1\u6709\u5f15\u7528\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u4f5c\u7528\u57df\u91cc\u7684\u53d8\u91cf\u00a0<code>outerVariable<\/code>\uff0c\u5b83\u4ec5\u4ec5\u6267\u884c\u4e86\u00a0<code>console.log(\"hello\");<\/code>\u00a0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7531\u4e8e\u4e0d\u6ee1\u8db3\u95ed\u5305\u5f62\u6210\u7684 \u201c\u5185\u90e8\u51fd\u6570\u5f15\u7528\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u201d \u8fd9\u4e00\u5173\u952e\u6761\u4ef6\uff0c\u6240\u4ee5&nbsp;<code>inner<\/code>&nbsp;\u51fd\u6570\u4e0d\u7b97\u95ed\u5305\u3002<\/p>\n\n\n\n<p><strong>\u5185\u5b58\u91ca\u653e\u7684\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let outerVariable = 'I am from outer function';\n    function inner() {\n        console.log(\"hello\");\n    }\n    return inner;\n}\u8fd9\u79cdouterVariable\u4f1a\u88ab\u5185\u5b58\u91ca\u653e\u6389\u5427\uff1f<\/pre>\n\n\n\n<p>\u5728\u4f60\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c<code>outerVariable<\/code>\u00a0\u53d8\u91cf\u4f1a\u5728\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u5185\u5b58\u91ca\u653e\u6389\u3002\u4e0b\u9762\u4ece JavaScript \u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3001\u95ed\u5305\u7279\u6027\u7b49\u65b9\u9762\u4e3a\u4f60\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n\n\n\n<p><strong>\u5783\u573e\u56de\u6536\u673a\u5236\u4e0e\u53d8\u91cf\u5f15\u7528<\/strong><\/p>\n\n\n\n<p>JavaScript \u5177\u5907\u81ea\u52a8\u7684\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u8be5\u673a\u5236\u901a\u5e38\u91c7\u7528\u6807\u8bb0\u6e05\u9664\u7b97\u6cd5\u6765\u5224\u65ad\u54ea\u4e9b\u53d8\u91cf\u53ef\u4ee5\u88ab\u56de\u6536\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u6ca1\u6709\u88ab\u4efb\u4f55\u6d3b\u52a8\u7684\u5f15\u7528\u6240\u6307\u5411\uff0c\u90a3\u4e48\u5b83\u5c31\u4f1a\u88ab\u6807\u8bb0\u4e3a\u53ef\u56de\u6536\u7684\u5bf9\u8c61\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\uff0c\u5783\u573e\u56de\u6536\u5668\u4f1a\u91ca\u653e\u8be5\u5bf9\u8c61\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u5f53\u524d\u4ee3\u7801\u4e2d\u00a0<code>outerVariable<\/code>\u00a0\u7684\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let outerVariable = 'I am from outer function';\n    function inner() {\n        console.log(\"hello\");\n    }\n    return inner;\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u91cc\uff1a<\/p>\n\n\n\n<ul>\n<li><code>outerVariable<\/code>\u00a0\u662f\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u53ea\u5728\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u57df\u5185\u6709\u6548\u3002<\/li>\n\n\n\n<li><code>inner<\/code>\u00a0\u51fd\u6570\u5e76\u6ca1\u6709\u5f15\u7528\u00a0<code>outerVariable<\/code>\u00a0\u53d8\u91cf\uff0c\u8fd9\u610f\u5473\u7740\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u4e2d\u4e0d\u5305\u542b\u5bf9\u00a0<code>outerVariable<\/code>\u00a0\u7684\u5f15\u7528\u3002<\/li>\n\n\n\n<li>\u5f53\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\uff0c\u5176\u6267\u884c\u4e0a\u4e0b\u6587\u4f1a\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\uff0c\u7531\u4e8e\u6ca1\u6709\u5176\u4ed6\u5730\u65b9\u5f15\u7528\u00a0<code>outerVariable<\/code>\uff0c\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u5c06\u5176\u6807\u8bb0\u4e3a\u53ef\u56de\u6536\u5bf9\u8c61\uff0c\u5e76\u5728\u540e\u7eed\u7684\u5783\u573e\u56de\u6536\u8fc7\u7a0b\u4e2d\u91ca\u653e\u5b83\u6240\u5360\u7528\u7684\u5185\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5bf9\u6bd4\u95ed\u5305\u60c5\u51b5<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u5f15\u7528\u4e86\u00a0<code>outerVariable<\/code>\uff0c\u90a3\u4e48\u60c5\u51b5\u5c31\u4f1a\u4e0d\u540c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let outerVariable = 'I am from outer function';\n    function inner() {\n        console.log(outerVariable);\n    }\n    return inner;\n}\nlet closure = outer();<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u95ed\u5305\u7684\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul>\n<li><code>inner<\/code>\u00a0\u51fd\u6570\u5f62\u6210\u4e86\u95ed\u5305\uff0c\u56e0\u4e3a\u5b83\u5f15\u7528\u4e86\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u00a0<code>outerVariable<\/code>\u3002<\/li>\n\n\n\n<li>\u5f53\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u867d\u7136\u5176\u6267\u884c\u4e0a\u4e0b\u6587\u4ece\u8c03\u7528\u6808\u4e2d\u79fb\u9664\uff0c\u4f46\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u4ecd\u7136\u4fdd\u7559\u7740\u5bf9\u00a0<code>outerVariable<\/code>\u00a0\u7684\u5f15\u7528\u3002<\/li>\n\n\n\n<li>\u53ea\u8981\u00a0<code>closure<\/code>\u00a0\u53d8\u91cf\u5b58\u5728\u5e76\u4e14\u5f15\u7528\u7740\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\uff0c<code>outerVariable<\/code>\u00a0\u5c31\u4e0d\u4f1a\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u56de\u6536\uff0c\u4f1a\u4e00\u76f4\u5b58\u5728\u4e8e\u5185\u5b58\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728\u6700\u521d\u63d0\u4f9b\u7684\u4ee3\u7801\u91cc\uff0c\u7531\u4e8e&nbsp;<code>inner<\/code>&nbsp;\u51fd\u6570\u672a\u5f15\u7528&nbsp;<code>outerVariable<\/code>\uff0c<code>outerVariable<\/code>&nbsp;\u4f1a\u5728&nbsp;<code>outer<\/code>&nbsp;\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u5185\u5b58\u91ca\u653e\u3002<\/p>\n\n\n\n<p><strong>\u5168\u90e8\u53d8\u91cf \u548c \u5c40\u90e8\u53d8\u91cf\u7684\u95ed\u5305<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li><strong>\u5c40\u90e8\u53d8\u91cf\u95ed\u5305<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var a = 5;\nfunction outer() {\n    function inner() {\n        console.log(a);\n    }\n    return inner;\n}\nlet fn = outer();\nfn();<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u00a0\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u662f\u6574\u4e2a\u5168\u5c40\u73af\u5883\uff0c\u751f\u547d\u5468\u671f\u4ece\u5b9a\u4e49\u5f00\u59cb\u76f4\u5230\u9875\u9762\u5173\u95ed\u6216\u8005\u5168\u5c40\u5bf9\u8c61\u88ab\u9500\u6bc1\u3002\u65e0\u8bba\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u662f\u5426\u6267\u884c\u5b8c\u6bd5\uff0c<code>a<\/code>\u00a0\u90fd\u4f1a\u4e00\u76f4\u5b58\u5728\u4e8e\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u6267\u884c\u72b6\u6001\u800c\u6539\u53d8\u5176\u751f\u547d\u5468\u671f\u3002<code>inner<\/code>\u00a0\u51fd\u6570\u5f62\u6210\u7684\u95ed\u5305\u53ea\u662f\u5f15\u7528\u4e86\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u00a0<code>a<\/code>\uff0c\u5bf9\u00a0<code>a<\/code>\u00a0\u7684\u751f\u547d\u5468\u671f\u6ca1\u6709\u5f71\u54cd\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u5c40\u90e8\u53d8\u91cf\u95ed\u5305<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let a = 10;\n    function inner() {\n        console.log(a);\n    }\n    return inner;\n}\nlet fn = outer();\nfn();<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684\u00a0<code>a<\/code>\u00a0\u662f\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u6309\u7167\u6b63\u5e38\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u5f53\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5176\u5c40\u90e8\u4f5c\u7528\u57df\u4f1a\u88ab\u9500\u6bc1\uff0c<code>a<\/code>\u00a0\u4e5f\u4f1a\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u56de\u6536\u3002\u4f46\u7531\u4e8e\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u5f62\u6210\u4e86\u95ed\u5305\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u5f15\u7528\u4e86\u00a0<code>a<\/code>\uff0c\u8fd9\u4f7f\u5f97\u00a0<code>a<\/code>\u00a0\u7684\u751f\u547d\u5468\u671f\u5f97\u4ee5\u5ef6\u957f\uff0c\u53ea\u8981\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u5b58\u5728\uff0c<code>a<\/code>\u00a0\u5c31\u4f1a\u4e00\u76f4\u5b58\u5728\u4e8e\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u53d8\u91cf\u7684\u53ef\u4fee\u6539\u6027\u548c\u72ec\u7acb\u6027<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5168\u5c40\u53d8\u91cf\u60c5\u51b5<\/strong>\uff1a\u7531\u4e8e\u00a0<code>a<\/code>\u00a0\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u8fd9\u610f\u5473\u7740\u9664\u4e86\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u4e4b\u5916\uff0c\u5176\u4ed6\u4ee3\u7801\u4e5f\u53ef\u80fd\u4f1a\u6539\u53d8\u00a0<code>a<\/code>\u00a0\u7684\u503c\uff0c\u4ece\u800c\u5f71\u54cd\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var a = 5;\nfunction outer() {\n    function inner() {\n        console.log(a);\n    }\n    return inner;\n}\nlet fn = outer();\na = 20; \/\/ \u5176\u4ed6\u4ee3\u7801\u4fee\u6539\u4e86\u5168\u5c40\u53d8\u91cf a \u7684\u503c\nfn(); \/\/ \u8f93\u51fa 20<\/pre>\n\n\n\n<ul>\n<li><strong>\u5c40\u90e8\u53d8\u91cf\u60c5\u51b5<\/strong>\uff1a<code>a<\/code>\u00a0\u662f\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u6709\u901a\u8fc7\u00a0<code>outer<\/code>\u00a0\u51fd\u6570\u6216\u8005\u00a0<code>inner<\/code>\u00a0\u51fd\u6570\uff08\u56e0\u4e3a\u95ed\u5305\u7684\u5b58\u5728\uff09\u624d\u80fd\u8bbf\u95ee\u548c\u4fee\u6539\u00a0<code>a<\/code>\u00a0\u7684\u503c\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u72ec\u7acb\u6027\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function outer() {\n    let a = 10;\n    function inner() {\n        console.log(a);\n    }\n    function modifyA() {\n        a = 20;\n    }\n    return {\n        inner: inner,\n        modifyA: modifyA\n    };\n}\nlet result = outer();\nresult.inner(); \/\/ \u8f93\u51fa 10\nresult.modifyA();\nresult.inner(); \/\/ \u8f93\u51fa 20<\/pre>\n\n\n\n<p><strong>\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f71\u54cd<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u5168\u5c40\u53d8\u91cf\u60c5\u51b5<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u672c\u8eab\u5c31\u4e00\u76f4\u5b58\u5728\u4e8e\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c<code>inner<\/code>\u00a0\u51fd\u6570\u5f62\u6210\u7684\u95ed\u5305\u53ea\u662f\u5f15\u7528\u4e86\u5168\u5c40\u53d8\u91cf\uff0c\u4e0d\u4f1a\u989d\u5916\u589e\u52a0\u592a\u591a\u7684\u5185\u5b58\u5f00\u9500\u3002\u4f46\u5982\u679c\u5168\u5c40\u53d8\u91cf\u8fc7\u591a\u6216\u8005\u6570\u636e\u91cf\u8fc7\u5927\uff0c\u4f1a\u5bfc\u81f4\u5168\u5c40\u4f5c\u7528\u57df\u7684\u5185\u5b58\u5360\u7528\u589e\u52a0\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u5c40\u90e8\u53d8\u91cf\u60c5\u51b5<\/strong>\uff1a\u7531\u4e8e\u95ed\u5305\u4f1a\u5ef6\u957f\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff0c\u4f7f\u5f97\u5c40\u90e8\u53d8\u91cf\u4e0d\u4f1a\u88ab\u53ca\u65f6\u56de\u6536\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u589e\u52a0\u3002\u5982\u679c\u5927\u91cf\u521b\u5efa\u8fd9\u79cd\u95ed\u5305\uff0c\u53ef\u80fd\u4f1a\u5bf9\u5185\u5b58\u6027\u80fd\u4ea7\u751f\u4e00\u5b9a\u7684\u5f71\u54cd\u3002\u4f46\u5728\u5408\u7406\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u5f71\u54cd\u662f\u53ef\u63a7\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u8fd9\u4e24\u79cd\u95ed\u5305\u5728\u53d8\u91cf\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u3001\u53ef\u4fee\u6539\u6027\u3001\u72ec\u7acb\u6027\u4ee5\u53ca\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f71\u54cd\u7b49\u65b9\u9762\u90fd\u5b58\u5728\u660e\u663e\u7684\u533a\u522b\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305\u5230\u5e95\u662f\u4ec0\u4e48 \u4eba\u4eba\u90fd\u5728\u8c08\u8bba\u95ed\u5305\uff08closure\uff09\uff0c\u8ba4\u4e3a\u5b83\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u8bed\u8a00\u7279\u6027\uff0c\u4f46\u6709\u591a\u5c11\u4eba\u771f\u6b63\u660e\u767d\u95ed\u5305\u662f\u4ec0\u4e48\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17234"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17234"}],"version-history":[{"count":9,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17234\/revisions"}],"predecessor-version":[{"id":17247,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17234\/revisions\/17247"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}