{"id":167630,"date":"2024-05-08T16:07:01","date_gmt":"2024-05-08T08:07:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167630.html"},"modified":"2024-05-08T16:07:07","modified_gmt":"2024-05-08T08:07:07","slug":"javascript%e9%97%ad%e5%8c%85%e4%bd%9c%e7%94%a8%e8%8c%83%e5%9b%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167630.html","title":{"rendered":"JavaScript\u95ed\u5305\u4f5c\u7528\u8303\u56f4"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27033604\/6374332d-fe24-4933-879b-081ee52bdb24.webp\" alt=\"JavaScript\u95ed\u5305\u4f5c\u7528\u8303\u56f4\" \/><\/p>\n<p><p>\u95ed\u5305\uff08Closure\uff09\u662f JavaScript \u4e2d\u7684\u9ad8\u7ea7\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u6307\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u4ee5\u53ca\u8be5\u51fd\u6570\u6240\u58f0\u660e\u65f6\u6240\u80fd\u8bbf\u95ee\u5230\u7684\u6240\u6709\u5916\u5c42\u53d8\u91cf\u7684\u7ec4\u5408\u3002<strong>\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u5305\u62ec\u6355\u83b7\u5916\u90e8\u53d8\u91cf\u3001\u4fdd\u6301\u79c1\u6709\u72b6\u6001\u3001\u5b9e\u73b0\u5c01\u88c5\u548c\u6a21\u5757\u5316<\/strong>\u3002\u95ed\u5305\u7684\u4f5c\u7528\u6700\u4e3a\u663e\u8457\u7684\u4e00\u70b9\u662f\u80fd\u521b\u5efa\u79c1\u6709\u53d8\u91cf\uff0c\u8fd9\u4e00\u7279\u6027\u4f7f\u5f97\u95ed\u5305\u5728\u6784\u5efa\u5177\u6709\u5c01\u88c5\u6027\u7684\u7ed3\u6784\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u95ed\u5305\u7684\u5b9a\u4e49\u4e0e\u539f\u7406<\/strong><\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u51fd\u6570\u548c\u58f0\u660e\u8be5\u51fd\u6570\u7684\u8bcd\u6cd5\u73af\u5883\u7684\u7ec4\u5408\u3002\u8bcd\u6cd5\u73af\u5883\u5305\u542b\u4e86\u95ed\u5305\u521b\u5efa\u65f6\u53ef\u7528\u7684\u6240\u6709\u672c\u5730\u53d8\u91cf\u3002\u5728 JavaScript \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><h4><strong>\u521b\u5efa\u95ed\u5305\u7684\u6761\u4ef6<\/strong><\/h4>\n<\/p>\n<ul>\n<li>\u5fc5\u987b\u6709\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u5fc5\u987b\u88ab\u5185\u90e8\u51fd\u6570\u6240\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p><h4><strong>\u95ed\u5305\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/h4>\n<\/p>\n<p><p>\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u5b9a\u4e49\u65f6\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u94fe\u5c31\u4f1a\u4fdd\u5b58\u6240\u6709\u7684\u5c40\u90e8\u53d8\u91cf\u503c\u3002\u5982\u679c\u8fd9\u4e2a\u51fd\u6570\u88ab\u53e6\u4e00\u4e2a\u51fd\u6570\u5f15\u7528\uff0c\u90a3\u4e48\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u5c06\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u5373\u4f7f\u5916\u5c42\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u95ed\u5305\u7684\u4f5c\u7528<\/strong><\/h3>\n<\/p>\n<p><h4><strong>\u6570\u636e\u5c01\u88c5\u548c\u79c1\u6709\u5316<\/strong><\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u5141\u8bb8\u5b9a\u4e49\u79c1\u6709\u53d8\u91cf\uff0c\u65e0\u6cd5\u4ece\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\uff0c\u53ea\u80fd\u901a\u8fc7\u95ed\u5305\u63d0\u4f9b\u7684\u51fd\u6570\u6765\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4><strong>\u4e3e\u4f8b\u8bf4\u660e<\/strong><\/h4>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\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>    return count++;<\/p>\n<p>  };<\/p>\n<p>}<\/p>\n<p>let counter = createCounter();<\/p>\n<p>console.log(counter()); \/\/ \u8f93\u51fa 0<\/p>\n<p>console.log(counter()); \/\/ \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf<code>count<\/code>\u5c31\u88ab\u5c01\u88c5\u5728\u95ed\u5305\u4e2d\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u5176\u503c\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<\/p>\n<p><h4><strong>\u4e8b\u4ef6\u5904\u7406\u5668<\/strong><\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u5e38\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0c\u4fdd\u6301\u5bf9\u4e8b\u4ef6\u89e6\u53d1\u65f6\u53d8\u91cf\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><h4><strong>\u4e3e\u4f8b\u8bf4\u660e<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u4e3a\u591a\u4e2a\u6309\u94ae\u7ed1\u5b9a\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function setupButtons() {<\/p>\n<p>  for (let i = 0; i &lt; buttons.length; i++) {<\/p>\n<p>    buttons[i].addEventListener(&#039;click&#039;, function() {<\/p>\n<p>      console.log(`Button ${i} clicked`);<\/p>\n<p>    });<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u90fd\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u8bb0\u4f4f\u4e86\u5f53\u524d\u5faa\u73af\u91cc\u7684<code>i<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u95ed\u5305\u7684\u5e38\u89c1\u8bef\u533a<\/strong><\/h3>\n<\/p>\n<p><h4><strong>\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898<\/strong><\/h4>\n<\/p>\n<p><p>\u5982\u679c\u95ed\u5305\u4e2d\u5f15\u7528\u7684\u5916\u90e8\u53d8\u91cf\u5f88\u5927\u6216\u8005\u751f\u547d\u5468\u671f\u5f88\u957f\uff0c\u90a3\u4e48\u5b83\u4eec\u5c06\u4e0d\u4f1a\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u6240\u6e05\u9664\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h4><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4e0d\u518d\u9700\u8981\u7684\u95ed\u5305\u80fd\u591f\u88ab\u5783\u573e\u56de\u6536\uff0c\u4f8b\u5982\u5c06\u95ed\u5305\u5b58\u50a8\u7684\u5f15\u7528\u8bbe\u4e3a<code>null<\/code>\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e94\u3001\u4f18\u5316\u95ed\u5305\u7684\u4f7f\u7528<\/strong><\/h3>\n<\/p>\n<p><h4><strong>\u51cf\u5c11\u95ed\u5305\u5360\u7528\u5185\u5b58<\/strong><\/h4>\n<\/p>\n<ul>\n<li>\u4e0d\u8981\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u95ed\u5305\u3002<\/li>\n<li>\u4f7f\u7528\u95ed\u5305\u65f6\u6ce8\u610f\u91ca\u653e\u5185\u5b58\u3002<\/li>\n<\/ul>\n<p><h4><strong>\u4e3e\u4f8b\u8bf4\u660e<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u4e0d\u9700\u8981\u8ba1\u6570\u5668\u65f6\uff0c\u53ef\u4ee5\u624b\u52a8\u91ca\u653e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">counter = null;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u5f15\u7528\u8bbe\u7f6e\u4e3a<code>null<\/code>\uff0c\u901a\u77e5\u5783\u573e\u56de\u6536\u5668\u95ed\u5305\u4e0d\u518d\u88ab\u9700\u8981\u3002<\/p>\n<\/p>\n<p><h3><strong>\u516d\u3001\u95ed\u5305\u7684\u9ad8\u7ea7\u5e94\u7528<\/strong><\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u8fd8\u53ef\u4ee5\u7528\u5728\u5982\u6a21\u5757\u6a21\u5f0f\u4e2d\uff0c\u901a\u8fc7\u95ed\u5305\u521b\u5efa\u79c1\u6709\u53d8\u91cf\uff0c\u53ea\u66b4\u9732\u51fa\u4e00\u4e2a\u63a5\u53e3\u4e0e\u5916\u90e8\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4><strong>\u4f7f\u7528\u6a21\u5757\u6a21\u5f0f<\/strong><\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">var Module = (function() {<\/p>\n<p>  var privateVariable = &#039;I am private&#039;;<\/p>\n<p>  var privateMethod = function() {<\/p>\n<p>    console.log(privateVariable);<\/p>\n<p>  };<\/p>\n<p>  return {<\/p>\n<p>    publicMethod: function() {<\/p>\n<p>      privateMethod();<\/p>\n<p>    }<\/p>\n<p>  };<\/p>\n<p>})();<\/p>\n<p>Module.publicMethod(); \/\/ \u8f93\u51fa &#039;I am private&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>privateVariable<\/code>\u548c<code>privateMethod<\/code>\u90fd\u65e0\u6cd5\u4ece\u6a21\u5757\u5916\u90e8\u8bbf\u95ee\uff0c\u53ea\u80fd\u901a\u8fc7\u66b4\u9732\u7684<code>publicMethod<\/code>\u65b9\u6cd5\u95f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e03\u3001\u95ed\u5305\u4e0eECMAScript\u89c4\u8303<\/strong><\/h3>\n<\/p>\n<p><p>ECMAScript \u7684\u7248\u672c\u66f4\u65b0\u7ed9\u95ed\u5305\u5e26\u6765\u4e86\u65b0\u7684\u7279\u6027\uff0c\u5982 ES6 \u5f15\u5165\u7684 let \u548c const \u5173\u952e\u5b57\u8ba9\u95ed\u5305\u5728\u5faa\u73af\u4e2d\u8868\u73b0\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5757\u7ea7\u4f5c\u7528\u57df\u4e0e\u95ed\u5305<\/strong><\/h4>\n<\/p>\n<p><p>ES6 \u4e2d\u7684<code>let<\/code>\u548c<code>const<\/code>\u80fd\u521b\u5efa\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u914d\u5408\u95ed\u5305\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4e0a\u6587\u7684\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\u5668\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>let<\/code>\u66ff\u6362<code>var<\/code>\u5c31\u80fd\u907f\u514d\u56e0\u95ed\u5305\u4ea7\u751f\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u95ed\u5305\u662f JavaScript \u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u7279\u6027\uff0c\u9002\u5f53\u8fd0\u7528\u53ef\u4ee5\u5e26\u6765\u8bb8\u591a\u7f16\u7a0b\u4e0a\u7684\u4fbf\u5229\u3002\u4f46\u540c\u65f6\u5b83\u4e5f\u9700\u8981\u5c0f\u5fc3\u5904\u7406\uff0c\u9632\u6b62\u51fa\u73b0\u5185\u5b58\u6cc4\u9732\u7b49\u95ee\u9898\u3002\u901a\u8fc7\u7406\u89e3\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u4e0e\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528\u95ed\u5305\u5e26\u6765\u7684\u597d\u5904\uff0c\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u3001\u66f4\u53ef\u9760\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: JavaScript\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>A: JavaScript\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u5305\u62ec\u51fd\u6570\u5185\u90e8\u7684\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4ee5\u53ca\u5b83\u4eec\u53ef\u4ee5\u8bbf\u95ee\u5230\u7684\u5916\u90e8\u53d8\u91cf\u548c\u51fd\u6570\u3002\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u548c\u5b58\u50a8\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5728\u5176\u5b9a\u4e49\u540e\u4ecd\u80fd\u8bbf\u95ee\u5230\u8fd9\u4e9b\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u7684\u6267\u884c\u5df2\u7ecf\u7ed3\u675f\u3002\u8fd9\u4f7f\u5f97\u95ed\u5305\u5728\u5904\u7406\u79c1\u6709\u53d8\u91cf\u3001\u6a21\u5757\u5316\u5f00\u53d1\u548c\u521b\u5efa\u7279\u5b9a\u4e0a\u4e0b\u6587\u7684\u56de\u8c03\u51fd\u6570\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u6b63\u786e\u4f7f\u7528JavaScript\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\uff1f<\/strong><\/p>\n<p>A: \u4e3a\u4e86\u6b63\u786e\u4f7f\u7528JavaScript\u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\uff0c\u6211\u4eec\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u3002\u9996\u5148\uff0c\u5e94\u8be5\u907f\u514d\u5728\u5faa\u73af\u4e2d\u521b\u5efa\u95ed\u5305\uff0c\u56e0\u4e3a\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u95ed\u5305\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u8fc7\u5ea6\u4f9d\u8d56\u95ed\u5305\uff0c\u4ee5\u514d\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002\u5728\u9700\u8981\u4f7f\u7528\u95ed\u5305\u7684\u5730\u65b9\uff0c\u8981\u786e\u4fdd\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u53d8\u91cf\u548c\u51fd\u6570\u5f15\u7528\u3002<\/p>\n<p><strong>Q: \u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u4e0e\u4f5c\u7528\u57df\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>A: \u95ed\u5305\u7684\u4f5c\u7528\u8303\u56f4\u4e0e\u4f5c\u7528\u57df\u662f\u76f8\u5173\u4f46\u4e0d\u5b8c\u5168\u76f8\u540c\u7684\u6982\u5ff5\u3002\u4f5c\u7528\u57df\u6307\u7684\u662f\u53d8\u91cf\u548c\u51fd\u6570\u7684\u53ef\u89c1\u6027\u548c\u8bbf\u95ee\u6027\uff0c\u800c\u95ed\u5305\u5219\u6307\u7684\u662f\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u548c\u4f7f\u7528\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u4f5c\u7528\u57df\u662f\u5728\u4ee3\u7801\u7f16\u8bd1\u65f6\u786e\u5b9a\u7684\uff0c\u800c\u95ed\u5305\u662f\u5728\u4ee3\u7801\u6267\u884c\u65f6\u521b\u5efa\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f5c\u7528\u57df\u662f\u9759\u6001\u7684\uff0c\u5728\u4ee3\u7801\u7f16\u5199\u65f6\u5c31\u56fa\u5b9a\u4e86\uff0c\u800c\u95ed\u5305\u662f\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u95ed\u5305\uff08Closure\uff09\u662f JavaScript \u4e2d\u7684\u9ad8\u7ea7\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u6307\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u4ee5\u53ca\u8be5\u51fd\u6570\u6240\u58f0\u660e\u65f6\u6240\u80fd\u8bbf\u95ee\u5230 [&hellip;]","protected":false},"author":3,"featured_media":167638,"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\/167630"}],"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=167630"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167630\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167638"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}