{"id":16,"date":"2026-06-16T22:43:36","date_gmt":"2026-06-16T14:43:36","guid":{"rendered":"https:\/\/io00.cn\/index.php\/2026\/06\/16\/%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-javascript-%e9%97%ad%e5%8c%85%ef%bc%9a%e4%bb%8e%e5%8e%9f%e7%90%86%e5%88%b0%e5%ae%9e%e8%b7%b5\/"},"modified":"2026-06-16T22:43:36","modified_gmt":"2026-06-16T14:43:36","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-javascript-%e9%97%ad%e5%8c%85%ef%bc%9a%e4%bb%8e%e5%8e%9f%e7%90%86%e5%88%b0%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/io00.cn\/index.php\/2026\/06\/16\/%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-javascript-%e9%97%ad%e5%8c%85%ef%bc%9a%e4%bb%8e%e5%8e%9f%e7%90%86%e5%88%b0%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3 JavaScript \u95ed\u5305\uff1a\u4ece\u539f\u7406\u5230\u5b9e\u8df5"},"content":{"rendered":"<p>\u95ed\u5305\u662f JavaScript \u4e2d\u6700\u91cd\u8981\u4e5f\u6700\u5bb9\u6613\u88ab\u8bef\u89e3\u7684\u6982\u5ff5\u4e4b\u4e00\u3002\u672c\u6587\u5c06\u4ece\u539f\u7406\u5165\u624b\uff0c\u6df1\u5165\u5256\u6790\u95ed\u5305\u7684\u672c\u8d28\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5c55\u793a\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u95ed\u5305\uff1f<\/h2>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u95ed\u5305\u5c31\u662f\u51fd\u6570\u80fd\u591f\u8bbf\u95ee\u5176\u8bcd\u6cd5\u4f5c\u7528\u57df\u5916\u90e8\u53d8\u91cf\u7684\u80fd\u529b\u3002\u66f4\u51c6\u786e\u5730\u8bf4\uff1a<\/p>\n<blockquote><p>\u95ed\u5305\u662f\u6307\u6709\u6743\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u7684\u51fd\u6570\u3002<\/p><\/blockquote>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-javascript\">function outer() {\n  const message = \"Hello, Closure!\";\n  \n  function inner() {\n    console.log(message);\n  }\n  \n  return inner;\n}\n\nconst myFunc = outer();\nmyFunc(); \/\/ \u8f93\u51fa: Hello, Closure!\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cinner \u51fd\u6570\u5c31\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u80fd\u591f\u8bbf\u95ee\u5916\u90e8\u51fd\u6570 outer \u4e2d\u7684 message \u53d8\u91cf\uff0c\u5373\u4f7f outer \u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<h2>\u95ed\u5305\u7684\u539f\u7406<\/h2>\n<p>\u8981\u7406\u89e3\u95ed\u5305\u7684\u539f\u7406\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3 JavaScript \u7684\u4f5c\u7528\u57df\u94fe\u548c\u6267\u884c\u4e0a\u4e0b\u6587\u3002<\/p>\n<h3>\u4f5c\u7528\u57df\u94fe<\/h3>\n<p>\u5f53 JavaScript \u5f15\u64ce\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u5e76\u5c06\u5176\u63a8\u5165\u6267\u884c\u6808\u3002\u6267\u884c\u4e0a\u4e0b\u6587\u5305\u542b\uff1a<\/p>\n<ol>\n<li><strong>\u53d8\u91cf\u5bf9\u8c61<\/strong>\uff1a\u5b58\u50a8\u51fd\u6570\u7684\u53c2\u6570\u3001\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\u3002<\/li>\n<li><strong>\u4f5c\u7528\u57df\u94fe<\/strong>\uff1a\u5f53\u524d\u4f5c\u7528\u57df\u548c\u6240\u6709\u7236\u7ea7\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u5bf9\u8c61\u7ec4\u6210\u7684\u94fe\u8868\u3002<\/li>\n<li><strong>this \u503c<\/strong>\uff1a\u51fd\u6570\u6267\u884c\u65f6\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<h3>\u95ed\u5305\u7684\u5f62\u6210<\/h3>\n<p>\u5f53\u5185\u90e8\u51fd\u6570\u88ab\u5176\u5916\u90e8\u51fd\u6570\u4e4b\u5916\u7684\u53d8\u91cf\u5f15\u7528\u65f6\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u95ed\u5305\u3002\u8fd9\u65f6\u5019\uff0c\u5185\u90e8\u51fd\u6570\u4f1a\u4fdd\u6301\u5bf9\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cmyFunc \u4ecd\u7136\u80fd\u591f\u8bbf\u95ee message \u53d8\u91cf\u7684\u539f\u56e0\u3002<\/p>\n<h2>\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<p>\u95ed\u5305\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u5f88\u591a\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f8b\u5b50\uff1a<\/p>\n<h3>1. \u6570\u636e\u79c1\u6709\u5316<\/h3>\n<p>\u5229\u7528\u95ed\u5305\u53ef\u4ee5\u521b\u5efa\u79c1\u6709\u53d8\u91cf\uff0c\u5b9e\u73b0\u6570\u636e\u5c01\u88c5\uff1a<\/p>\n<pre><code class=\"language-javascript\">function createCounter() {\n  let count = 0;\n  \n  return {\n    increment() {\n      count++;\n      return count;\n    },\n    decrement() {\n      count--;\n      return count;\n    },\n    getCount() {\n      return count;\n    }\n  };\n}\n\nconst counter = createCounter();\nconsole.log(counter.increment()); \/\/ 1\nconsole.log(counter.increment()); \/\/ 2\nconsole.log(counter.getCount());  \/\/ 2\nconsole.log(counter.count);       \/\/ undefined\n<\/code><\/pre>\n<h3>2. \u51fd\u6570\u67ef\u91cc\u5316<\/h3>\n<p>\u67ef\u91cc\u5316\u662f\u4e00\u79cd\u5c06\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u8f6c\u6362\u4e3a\u4e00\u7cfb\u5217\u63a5\u53d7\u5355\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u7684\u6280\u672f\uff1a<\/p>\n<pre><code class=\"language-javascript\">function curry(fn) {\n  return function curried(...args) {\n    if (args.length &gt;= fn.length) {\n      return fn.apply(this, args);\n    } else {\n      return function(...nextArgs) {\n        return curried.apply(this, args.concat(nextArgs));\n      };\n    }\n  };\n}\n\nconst add = (a, b, c) =&gt; a + b + c;\nconst curriedAdd = curry(add);\n\nconsole.log(curriedAdd(1)(2)(3)); \/\/ 6\nconsole.log(curriedAdd(1, 2)(3)); \/\/ 6\nconsole.log(curriedAdd(1)(2, 3)); \/\/ 6\n<\/code><\/pre>\n<h3>3. \u6a21\u5757\u5316<\/h3>\n<p>\u5728 ES6 \u6a21\u5757\u51fa\u73b0\u4e4b\u524d\uff0c\u95ed\u5305\u662f\u5b9e\u73b0\u6a21\u5757\u5316\u7684\u4e3b\u8981\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-javascript\">const module = (function() {\n  const privateVar = \"I am private\";\n  \n  function privateMethod() {\n    return privateVar;\n  }\n  \n  return {\n    publicMethod() {\n      return privateMethod();\n    },\n    publicVar: \"I am public\"\n  };\n})();\n\nconsole.log(module.publicVar);      \/\/ \"I am public\"\nconsole.log(module.publicMethod()); \/\/ \"I am private\"\nconsole.log(module.privateVar);     \/\/ undefined\n<\/code><\/pre>\n<h2>\u95ed\u5305\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>\u867d\u7136\u95ed\u5305\u5f88\u5f3a\u5927\uff0c\u4f46\u4f7f\u7528\u4e0d\u5f53\u4e5f\u4f1a\u5e26\u6765\u95ee\u9898\uff1a<\/p>\n<h3>\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<p>\u95ed\u5305\u4f1a\u4fdd\u6301\u5bf9\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\uff0c\u5982\u679c\u95ed\u5305\u88ab\u957f\u671f\u6301\u6709\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<h3>\u6027\u80fd\u95ee\u9898<\/h3>\n<p>\u95ed\u5305\u4f1a\u589e\u52a0\u5185\u5b58\u5f00\u9500\uff0c\u56e0\u4e3a\u9700\u8981\u4fdd\u5b58\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u3002\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u666f\u4e2d\u9700\u8981\u6ce8\u610f\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u95ed\u5305\u662f JavaScript \u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7406\u89e3\u95ed\u5305\u6709\u52a9\u4e8e\u6211\u4eec\u5199\u51fa\u66f4\u4f18\u96c5\u3001\u66f4\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u8bb0\u4f4f\uff1a<\/p>\n<ul>\n<li>\u95ed\u5305\u662f\u6709\u6743\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u7684\u51fd\u6570<\/li>\n<li>\u95ed\u5305\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u79c1\u6709\u5316\u3001\u51fd\u6570\u67ef\u91cc\u5316\u548c\u6a21\u5757\u5316<\/li>\n<li>\u4f7f\u7528\u95ed\u5305\u65f6\u8981\u6ce8\u610f\u5185\u5b58\u6cc4\u6f0f\u548c\u6027\u80fd\u95ee\u9898<\/li>\n<\/ul>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u95ed\u5305\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ed\u5305\u662f JavaScript \u4e2d\u6700\u91cd\u8981\u4e5f\u6700\u5bb9\u6613\u88ab\u8bef\u89e3\u7684\u6982\u5ff5\u4e4b\u4e00\u3002\u672c\u6587\u5c06\u4ece\u539f\u7406\u5165\u624b\uff0c\u6df1\u5165\u5256\u6790\u95ed\u5305\u7684\u672c\u8d28\uff0c\u5e76\u901a\u8fc7\u5b9e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19,21,20],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-javascript","tag-21","tag-20"],"_links":{"self":[{"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/io00.cn\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}