{"id":170832,"date":"2024-05-08T17:16:12","date_gmt":"2024-05-08T09:16:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170832.html"},"modified":"2024-05-08T17:16:14","modified_gmt":"2024-05-08T09:16:14","slug":"javascript-%e9%97%ad%e5%8c%85%e5%87%bd%e6%95%b0%e5%8e%9f%e7%90%86%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/170832.html","title":{"rendered":"JavaScript \u95ed\u5305\u51fd\u6570\u539f\u7406\u6709\u54ea\u4e9b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27041728\/0ab1cc70-8fe5-429c-8b48-514ff861df70.webp\" alt=\"JavaScript \u95ed\u5305\u51fd\u6570\u539f\u7406\u6709\u54ea\u4e9b\" \/><\/p>\n<p><p>\u95ed\u5305\uff08Closure\uff09\u662fJavaScript\u8bed\u8a00\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c<strong>\u5b83\u662f\u51fd\u6570\u548c\u58f0\u660e\u8be5\u51fd\u6570\u7684\u8bcd\u6cd5\u73af\u5883\u7684\u7ec4\u5408<\/strong>\u3001<strong>\u53ef\u4ee5\u8ba9\u51fd\u6570\u8bbf\u95ee\u5e76\u64cd\u4f5c\u51fd\u6570\u5916\u7684\u53d8\u91cf<\/strong>\u3002\u5176\u539f\u7406\u4e3b\u8981\u5305\u62ec<strong>\u51fd\u6570\u4f5c\u7528\u57df\u3001\u8bcd\u6cd5\u4f5c\u7528\u57df\u94fe\u4ee5\u53ca\u53d8\u91cf\u7684\u6301\u4e45\u5316<\/strong>\u3002\u95ed\u5305\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u5728\u5176\u5b9a\u4e49\u65f6\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u6267\u884c\u65f6\uff0c\u80fd\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u5230\u5176\u4f5c\u7528\u57df\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u5728JavaScript\u4e2d\uff0c\u6bcf\u5f53\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u95ed\u5305\u5c31\u4f1a\u5728\u51fd\u6570\u521b\u5efa\u7684\u540c\u65f6\u88ab\u521b\u5efa\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u95ed\u5305\u7684\u4ea7\u751f\u57fa\u4e8eJavaScript\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u51fd\u6570\u5728\u6267\u884c\u65f6\u4f1a\u67e5\u627e\u5728\u5176\u8bcd\u6cd5\u4f5c\u7528\u57df\u5185\u58f0\u660e\u7684\u53d8\u91cf\u3002\u5f53\u51fd\u6570\u53ef\u4ee5\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u6240\u5728\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u65f6\uff0c\u5373\u4f7f\u8be5\u51fd\u6570\u5728\u5176\u8bcd\u6cd5\u4f5c\u7528\u57df\u5916\u6267\u884c\uff0c\u95ed\u5305\u4e5f\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u53d8\u91cf\u4ecd\u7136\u88ab\u8bbf\u95ee\uff0c\u4ece\u800c\u8fd9\u4e9b\u5c40\u90e8\u53d8\u91cf\u4e0d\u4f1a\u968f\u7740\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u800c\u88ab\u9500\u6bc1\uff0c\u5b83\u4eec\u7684\u72b6\u6001\u5f97\u4ee5\u4fdd\u7559\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u95ed\u5305\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u5173\u95ed\u662f\u6307\u51fd\u6570<strong>\u5728\u5b9a\u4e49\u65f6\u6355\u83b7<\/strong>\u5176\u6240\u5728\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u5e76\u4e14\u53ea\u8981\u8fd9\u4e2a\u51fd\u6570\u5b58\u5728\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5c31\u4f1a\u88ab\u4fdd\u7559\u2014\u2014\u8fd9\u662f\u95ed\u5305\u7684\u6838\u5fc3\u3002\u95ed\u5305\u7684\u5de5\u4f5c\u539f\u7406\u5728\u4e8eJavaScript\u7684\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u5728\u51fd\u6570\u5185\u90e8\uff0c\u5bf9\u4e8e\u5185\u90e8\u51fd\u6570\u6765\u8bf4\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5230\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u8ba9\u95ed\u5305\u6210\u4e3a\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u6784\u5efa\u62bd\u8c61\u7ed3\u6784\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u95ed\u5305\u7684\u5e38\u89c1\u7528\u9014<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u662f<strong>\u5b9e\u73b0\u79c1\u6709\u53d8\u91cf<\/strong>\u3002\u901a\u8fc7\u95ed\u5305\uff0c\u53ef\u4ee5\u5728JavaScript\u4e2d\u6a21\u62df\u51fa\u5757\u4f5c\u7528\u57df\uff0c\u8ba9\u51fd\u6570\u8bbf\u95ee\u7279\u5b9a\u53d8\u91cf\uff0c\u540c\u65f6\u963b\u6b62\u5168\u5c40\u4f5c\u7528\u57df\u7684\u8bbf\u95ee\uff0c\u8fbe\u5230\u5c01\u88c5\u548c\u4fdd\u62a4\u6570\u636e\u7684\u6548\u679c\u3002\u6b64\u5916\uff0c\u95ed\u5305\u5e38\u7528\u4e8e<strong>\u56de\u8c03\u51fd\u6570\u548c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/strong>\u3001<strong>\u5b9e\u73b0\u6a21\u5757\u6a21\u5f0f<\/strong>\uff0c\u4ee5\u53ca<strong>\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\u4fdd\u6301\u53d8\u91cf\u7684\u72b6\u6001<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u95ed\u5305\u548c\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u95ed\u5305\u4f1a\u6301\u6709\u5b83\u4eec\u521b\u5efa\u65f6\u8bcd\u6cd5\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u5982\u679c\u4e0d\u518d\u9700\u8981\u8fd9\u4e9b\u95ed\u5305\uff0c\u5e94\u5f53\u5c06\u5b83\u4eec\u8d4b\u503c\u4e3anull\u4ee5\u91ca\u653e\u8d44\u6e90\u3002\u7ba1\u7406\u95ed\u5305\u6d89\u53ca<strong>\u5408\u7406\u89c4\u5212\u751f\u547d\u5468\u671f<\/strong>\u548c<strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u95ed\u5305\u4fdd\u7559<\/strong>\uff0c\u53ea\u6709\u8fd9\u6837\uff0c\u624d\u80fd\u5728\u4eab\u53d7\u95ed\u5305\u63d0\u4f9b\u7684\u4fbf\u5229\u548c\u529f\u80fd\u7684\u540c\u65f6\uff0c\u907f\u514d\u5bf9\u7a0b\u5e8f\u6027\u80fd\u9020\u6210\u8d1f\u9762\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u73b0\u95ed\u5305\u7684\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\u521b\u5efa\u95ed\u5305\u5f88\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u95ed\u5305\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function createCounter() {<\/p>\n<p>    let count = 0;<\/p>\n<p>    return function () {<\/p>\n<p>        count += 1;<\/p>\n<p>        return count;<\/p>\n<p>    };<\/p>\n<p>}<\/p>\n<p>const counter = createCounter();<\/p>\n<p>console.log(counter()); \/\/ \u8f93\u51fa 1<\/p>\n<p>console.log(counter()); \/\/ \u8f93\u51fa 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>createCounter<\/code>\u51fd\u6570\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee<code>count<\/code>\u53d8\u91cf\uff0c\u4f46\u901a\u8fc7\u95ed\u5305\uff0c\u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u80fd\u591f\u8bbf\u95ee<code>createCounter<\/code>\u5185\u90e8\u7684<code>count<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7406\u89e3\u8bcd\u6cd5\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u548c\u8bcd\u6cd5\u4f5c\u7528\u57df\u5bc6\u4e0d\u53ef\u5206\u3002\u8bcd\u6cd5\u4f5c\u7528\u57df\u662f\u6307\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u4f9d\u8d56\u4e8e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff0c\u8fd9\u4e2a\u4f5c\u7528\u57df\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\uff0c\u4e0d\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\u786e\u5b9a\u3002JavaScript\u8fd0\u884c\u65f6\uff0c\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u67e5\u627e\u9075\u5faa\u8bcd\u6cd5\u4f5c\u7528\u57df\u94fe\uff0c\u8fd9\u5c31\u662f\u95ed\u5305\u80fd\u591f\u8bbf\u95ee\u51fd\u6570\u5916\u90e8\u53d8\u91cf\u7684\u539f\u7406\u6240\u5728\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u95ed\u5305\u9020\u6210\u7684\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u95ed\u5305\u662f\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\uff0c\u4f46\u5982\u679c\u4e0d\u5408\u7406\u4f7f\u7528\u4e5f\u4f1a\u5f15\u8d77\u5185\u5b58\u6cc4\u6f0f\u3001\u4ee3\u7801\u6a21\u7cca\u4e0d\u6e05\u7b49\u95ee\u9898\u3002\u5bf9\u6b64\uff0c\u5f00\u53d1\u8005\u9700\u8981<strong>\u6ce8\u610f\u95ed\u5305\u7684\u751f\u547d\u5468\u671f<\/strong>\u3001<strong>\u907f\u514d\u5728\u4e0d\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u95ed\u5305<\/strong>\uff0c\u5e76\u5728\u4e0d\u518d\u4f7f\u7528\u95ed\u5305\u65f6\u53ca\u65f6\u65ad\u5f00\u5f15\u7528\u3002\u4ee3\u7801\u7684\u64b0\u5199\u5e94\u5f53\u660e\u6670\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\uff0c\u4ee5\u514d\u589e\u52a0\u4ee3\u7801\u7ef4\u62a4\u7684\u96be\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u95ed\u5305\u662fJavaScript\u7684\u6838\u5fc3\u6982\u5ff5\u4e4b\u4e00\uff0c\u5176\u5f3a\u5927\u529f\u80fd\u5728\u8bb8\u591aJavaScript\u7a0b\u5e8f\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u90fd\u6709\u5e94\u7528\u3002\u6b63\u786e\u5730\u7406\u89e3\u548c\u4f7f\u7528\u95ed\u5305\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u589e\u5f3a\u7a0b\u5e8f\u7684\u8868\u73b0\u529b\u548c\u6a21\u5757\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fJavaScript\u95ed\u5305\u51fd\u6570\uff1f<\/strong><br \/>\nJavaScript\u95ed\u5305\u51fd\u6570\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5176\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3001\u53c2\u6570\u548c\u51fd\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u7ee7\u7eed\u5b58\u5728\u3002<\/p>\n<p><strong>2. \u95ed\u5305\u51fd\u6570\u6709\u4ec0\u4e48\u4f5c\u7528\u548c\u4f18\u52bf\uff1f<\/strong><br \/>\n\u95ed\u5305\u51fd\u6570\u7684\u4f5c\u7528\u4e4b\u4e00\u662f\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5c01\u88c5\u548c\u9690\u85cf\uff0c\u5c06\u4e00\u4e9b\u79c1\u6709\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u53ea\u66b4\u9732\u51fa\u9700\u8981\u7684\u90e8\u5206\u3002\u53e6\u5916\uff0c\u95ed\u5305\u51fd\u6570\u8fd8\u53ef\u4ee5\u5ef6\u957f\u5916\u90e8\u51fd\u6570\u5185\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\uff0c\u5185\u90e8\u51fd\u6570\u8fd8\u662f\u53ef\u4ee5\u7ee7\u7eed\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u95ed\u5305\u51fd\u6570\u8fd8\u6709\u4e00\u4e2a\u4f18\u52bf\u662f\u53ef\u4ee5\u5b9e\u73b0\u51fd\u6570\u7684\u67ef\u91cc\u5316\uff0c\u5373\u5c06\u591a\u53c2\u6570\u7684\u51fd\u6570\u8f6c\u5316\u4e3a\u63a5\u53d7\u5355\u4e00\u53c2\u6570\u7684\u591a\u4e2a\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u4f18\u96c5\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u6b63\u786e\u4f7f\u7528\u95ed\u5305\u51fd\u6570\uff1f<\/strong><br \/>\n\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u95ee\u9898\u3002\u9996\u5148\u662f\u5185\u5b58\u6cc4\u6f0f\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u95ed\u5305\u51fd\u6570\u4f1a\u4f7f\u5916\u90e8\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4e00\u76f4\u5b58\u5728\u4e8e\u5185\u5b58\u4e2d\uff0c\u6240\u4ee5\u5982\u679c\u4e0d\u518d\u9700\u8981\u4f7f\u7528\u95ed\u5305\u51fd\u6570\uff0c\u9700\u8981\u624b\u52a8\u89e3\u9664\u5f15\u7528\uff0c\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u7684\u53d1\u751f\u3002<\/p>\n<p>\u5176\u6b21\u662f\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u95ee\u9898\uff0c\u56e0\u4e3a\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u6240\u4ee5\u9700\u8981\u6ce8\u610f\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u8303\u56f4\uff0c\u907f\u514d\u51fa\u73b0\u610f\u5916\u7684bug\u3002<\/p>\n<p>\u6700\u540e\u662f\u95ed\u5305\u51fd\u6570\u7684\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u4e3a\u95ed\u5305\u51fd\u6570\u4f1a\u4e00\u76f4\u4fdd\u5b58\u5bf9\u5916\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u8fd9\u53ef\u80fd\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u8017\u3002\u6240\u4ee5\u5728\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u7684\u65f6\u5019\uff0c\u8981\u5728\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u4e4b\u95f4\u8fdb\u884c\u6743\u8861\uff0c\u907f\u514d\u6ee5\u7528\u95ed\u5305\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u95ed\u5305\uff08Closure\uff09\u662fJavaScript\u8bed\u8a00\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b83\u662f\u51fd\u6570\u548c\u58f0\u660e\u8be5\u51fd\u6570\u7684\u8bcd\u6cd5\u73af\u5883\u7684\u7ec4\u5408\u3001\u53ef\u4ee5\u8ba9\u51fd [&hellip;]","protected":false},"author":3,"featured_media":170835,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170832"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=170832"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170832\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/170835"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}