{"id":2093,"date":"2025-02-27T23:38:06","date_gmt":"2025-02-27T15:38:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2093"},"modified":"2025-02-27T23:38:06","modified_gmt":"2025-02-27T15:38:06","slug":"javascript-%e9%97%ad%e5%8c%85","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/27\/javascript-%e9%97%ad%e5%8c%85\/","title":{"rendered":"JavaScript \u95ed\u5305"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c\u95ed\u5305\uff08Closure\uff09\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u6307\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u201c\u8bb0\u4f4f\u201d\u5e76\u8bbf\u95ee\u5b83\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u95ed\u5305\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8bbf\u95ee\u5176\u5916\u90e8\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\u3002<\/p>\n\n\n\n<p>\u95ed\u5305\u7684\u51fa\u73b0\u662f\u56e0\u4e3a JavaScript \u4e2d\u7684\u51fd\u6570\u4f5c\u7528\u57df\u662f\u57fa\u4e8e\u5d4c\u5957\u7684\u3002\u5f53\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u5b83\u7684\u5c40\u90e8\u53d8\u91cf\u901a\u5e38\u4f1a\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u5982\u679c\u8fd9\u4e2a\u51fd\u6570\u5185\u90e8\u6709\u5176\u4ed6\u51fd\u6570\u5f15\u7528\u4e86\u8fd9\u4e9b\u5c40\u90e8\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5c40\u90e8\u53d8\u91cf\u5c31\u4e0d\u4f1a\u88ab\u9500\u6bc1\uff0c\u5f62\u6210\u4e86\u95ed\u5305\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001<strong>\u95ed\u5305\u7684\u7279\u70b9\uff1a<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u80fd\u591f\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf<\/strong>\uff1a\u95ed\u5305\u5141\u8bb8\u5185\u90e8\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/li>\n\n\n\n<li><strong>\u6301\u4e45\u5316\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<\/strong>\uff1a\u95ed\u5305\u80fd\u201c\u8bb0\u4f4f\u201d\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u6b64\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u6267\u884c\u7ed3\u675f\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4f9d\u7136\u5b58\u5728\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001<strong>\u95ed\u5305\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<p>\u95ed\u5305\u901a\u5e38\u662f\u7531\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\u6240\u4ea7\u751f\u7684\u3002\u8fd4\u56de\u7684\u51fd\u6570\u80fd\u591f\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5c06\u88ab\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u76f4\u5230\u95ed\u5305\u4e0d\u518d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function outer() {\n  let outerVar = 'I am from outer';\n\n  \/\/ inner function is a closure\n  function inner() {\n    console.log(outerVar);\n  }\n\n  return inner; \/\/ \u8fd4\u56de\u5185\u51fd\u6570\uff0c\u5f62\u6210\u95ed\u5305\n}\n\nconst closureFunction = outer();\nclosureFunction();  \/\/ \u8f93\u51fa: I am from outer<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>inner<\/code> \u51fd\u6570\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8bbf\u95ee <code>outer<\/code> \u51fd\u6570\u7684 <code>outerVar<\/code> \u53d8\u91cf\u3002\u5373\u4f7f <code>outer<\/code> \u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c<code>inner<\/code> \u51fd\u6570\u4ecd\u7136\u80fd\u591f\u8bbf\u95ee\u5230 <code>outerVar<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001<strong>\u95ed\u5305\u7684\u5e94\u7528\uff1a<\/strong><\/h3>\n\n\n\n<p>\u95ed\u5305\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u6570\u636e\u5c01\u88c5\uff08\u79c1\u6709\u53d8\u91cf\uff09<\/strong><\/h4>\n\n\n\n<p>\u95ed\u5305\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u79c1\u6709\u53d8\u91cf\uff0c\u56e0\u4e3a JavaScript \u4e0d\u652f\u6301\u4f20\u7edf\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u79c1\u6709\u53d8\u91cf\u3002\u901a\u8fc7\u95ed\u5305\uff0c\u53ef\u4ee5\u5c06\u67d0\u4e9b\u6570\u636e\u5c01\u88c5\u8d77\u6765\uff0c\u4e0d\u88ab\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function counter() {\n  let count = 0; \/\/ \u79c1\u6709\u53d8\u91cf\n\n  return {\n    increment: function() {\n      count++;\n      console.log(count);\n    },\n    decrement: function() {\n      count--;\n      console.log(count);\n    },\n    getCount: function() {\n      return count;\n    }\n  };\n}\n\nconst counter1 = counter();\ncounter1.increment();  \/\/ \u8f93\u51fa: 1\ncounter1.increment();  \/\/ \u8f93\u51fa: 2\nconsole.log(counter1.getCount());  \/\/ \u8f93\u51fa: 2<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code> \u662f\u79c1\u6709\u7684\uff0c\u65e0\u6cd5\u4ece\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7 <code>increment<\/code> \u548c <code>decrement<\/code> \u65b9\u6cd5\u6765\u4fee\u6539\u5b83\uff0c<code>getCount<\/code> \u65b9\u6cd5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u7684\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u5ef6\u8fdf\u6267\u884c \/ \u60f0\u6027\u52a0\u8f7d<\/strong><\/h4>\n\n\n\n<p>\u95ed\u5305\u5e38\u5e38\u7528\u4e8e\u5ef6\u8fdf\u6267\u884c\u7684\u573a\u666f\uff0c\u4f8b\u5982\uff0c\u5ef6\u8fdf\u6267\u884c\u4e00\u4e2a\u51fd\u6570\u6216\u521b\u5efa\u4e00\u4e2a\u6309\u9700\u52a0\u8f7d\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createFunction() {\n  let value = 0;\n\n  return function() {\n    value++;\n    console.log(value);\n  };\n}\n\nconst delayedFunction = createFunction();\ndelayedFunction();  \/\/ \u8f93\u51fa: 1\ndelayedFunction();  \/\/ \u8f93\u51fa: 2\ndelayedFunction();  \/\/ \u8f93\u51fa: 3<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>createFunction<\/code> \u8fd4\u56de\u7684\u95ed\u5305\u51fd\u6570\u80fd\u591f\u8bb0\u4f4f\u5e76\u9012\u589e <code>value<\/code> \u53d8\u91cf\u7684\u503c\uff0c\u6bcf\u6b21\u8c03\u7528\u65f6\u90fd\u4f1a\u589e\u52a0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u56de\u8c03\u51fd\u6570\u4e0e\u95ed\u5305<\/strong><\/h4>\n\n\n\n<p>\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u95ed\u5305\u5e38\u7528\u4e8e\u56de\u8c03\u51fd\u6570\u4e2d\u3002\u7531\u4e8e\u56de\u8c03\u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u67d0\u4e2a\u65f6\u95f4\u70b9\u5f02\u6b65\u6267\u884c\uff0c\u95ed\u5305\u5141\u8bb8\u56de\u8c03\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function fetchData(callback) {\n  let data = \"Data fetched!\";\n  callback(data);  \/\/ \u95ed\u5305\u5728\u8fd9\u91cc\u88ab\u4f7f\u7528\n}\n\nfetchData(function(result) {\n  console.log(result);  \/\/ \u8f93\u51fa: Data fetched!\n});<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee <code>fetchData<\/code> \u51fd\u6570\u7684 <code>data<\/code> \u53d8\u91cf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001<strong>\u95ed\u5305\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898<\/strong>\uff1a\u7531\u4e8e\u95ed\u5305\u53ef\u4ee5\u6301\u7eed\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u7279\u522b\u662f\u5728\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u6e05\u7406\u95ed\u5305\u7684\u5f15\u7528\uff0c\u53d8\u91cf\u4f1a\u4e00\u76f4\u88ab\u5360\u7528\u3002\u8981\u6ce8\u610f\u9002\u65f6\u91ca\u653e\u95ed\u5305\u5f15\u7528\u3002<\/li>\n\n\n\n<li><strong>\u7406\u89e3 <code>this<\/code><\/strong>\uff1a\u95ed\u5305\u4e2d\u7684 <code>this<\/code> \u6307\u5411\u5e76\u4e0d\u662f\u521b\u5efa\u95ed\u5305\u7684\u51fd\u6570\u4e2d\u7684 <code>this<\/code>\uff0c\u800c\u662f\u8c03\u7528\u95ed\u5305\u65f6\u6240\u4f9d\u8d56\u7684\u4e0a\u4e0b\u6587\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u7531\u4e8e\u95ed\u5305\u4f1a\u4f7f\u51fd\u6570\u4f53\u5185\u7684\u53d8\u91cf\u5f97\u4ee5\u4fdd\u6301\uff0c\u591a\u4e2a\u95ed\u5305\u521b\u5efa\u8fc7\u591a\u7684\u53d8\u91cf\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u8981\u8c28\u614e\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u95ed\u5305\u662f\u51fd\u6570\u4e0e\u5176\u76f8\u5173\u4f5c\u7528\u57df\u7684\u7ec4\u5408<\/strong>\uff0c\u53ef\u4ee5\u8ba9\u5185\u90e8\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u89c1\u7528\u9014<\/strong>\u5305\u62ec\u6570\u636e\u5c01\u88c5\u3001\u5ef6\u8fdf\u6267\u884c\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u95ed\u5305\u65f6\u8981\u6ce8\u610f<strong>\u5185\u5b58\u6cc4\u6f0f\u548c\u6027\u80fd\u95ee\u9898<\/strong>\uff0c\u786e\u4fdd\u53d8\u91cf\u4e0d\u4f1a\u5728\u4e0d\u9700\u8981\u65f6\u5360\u7528\u5185\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u95ed\u5305\uff08Closure\uff09\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u6307\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u201c\u8bb0\u4f4f\u201d\u5e76\u8bbf\u95ee\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2093","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2093"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2093\/revisions"}],"predecessor-version":[{"id":2094,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2093\/revisions\/2094"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}