{"id":3352,"date":"2024-11-10T13:01:55","date_gmt":"2024-11-10T05:01:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3352\/"},"modified":"2024-11-10T13:01:55","modified_gmt":"2024-11-10T05:01:55","slug":"%e5%a6%82%e4%bd%95%e5%b0%81%e8%a3%85%e8%87%aa%e5%b7%b1%e7%9a%84js%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3352\/","title":{"rendered":"\u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684js\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u5c01\u88c5\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u79c1\u6709\u4f5c\u7528\u57df\uff0c\u901a\u8fc7\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\u5b9e\u73b0\uff1a\u5de5\u5382\u6a21\u5f0f\uff1a\u521b\u5efa\u5305\u542b\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u3002\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (iife)\uff1a\u5c06\u51fd\u6570\u4f5c\u4e3a\u7acb\u5373\u8c03\u7528\u7684\u533f\u540d\u51fd\u6570\u7684\u53c2\u6570\uff0c\u51fd\u6570\u5728\u5185\u90e8\u88ab\u8c03\u7528\uff0c\u8fd4\u56de\u5e26\u6709\u79c1\u6709\u4f5c\u7528\u57df\u7684\u5bf9\u8c61\u6216\u95ed\u5305\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110810215654127.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684js\u51fd\u6570\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684js\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684 JavaScript \u51fd\u6570<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u5c01\u88c5\u51fd\u6570\u662f\u521b\u5efa\u4e00\u4e2a\u79c1\u6709\uff0c\u4f7f\u51fd\u6570\u5185\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u4ec5\u5728\u51fd\u6570\u5185\u90e8\u53ef\u89c1\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\uff1a<\/strong>\u53ef\u4ee5\u9690\u85cf\u51fd\u6570\u7684\u5185\u90e8\u5de5\u4f5c\u539f\u7406\uff0c\u8ba9\u7528\u6237\u53ea\u5173\u6ce8\u516c\u5f00\u7684\u63a5\u53e3\u3002<\/li>\n<li> <strong>\u9632\u6b62\u51b2\u7a81\uff1a<\/strong>\u53ef\u4ee5\u9632\u6b62\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u548c\u51fd\u6570\u540d\u79f0\u4e0e\u5176\u4ed6\u4ee3\u7801\u51b2\u7a81\u3002<\/li>\n<li> <strong>\u6a21\u5757\u5316\uff1a<\/strong>\u53ef\u4ee5\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u4fbf\u4e8e\u91cd\u7528\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p><strong>\u5c01\u88c5\u51fd\u6570\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u5c01\u88c5 JavaScript \u51fd\u6570\uff1a<\/p>\n<p><strong>1. \u5de5\u5382\u6a21\u5f0f<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u3002<\/li>\n<li>\u5bf9\u8c61\u7684\u5185\u90e8\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u79c1\u6709\u7684\u3002<\/li>\n<li>\u5bf9\u8c61\u7684\u5916\u90e8\u63a5\u53e3\u662f\u901a\u8fc7\u516c\u5f00\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u8bbf\u95ee\u7684\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function createCounter() {\n  let count = 0;\n\n  return {\n    increment: function() {\n      count++;\n    },\n    decrement: function() {\n      count--;\n    },\n    getCount: function() {\n      return count;\n    }\n  };\n}\n\nconst counter = createCounter();\ncounter.increment();\nconsole.log(counter.getCount()); \/\/ 1\nconsole.log(counter.count); \/\/ undefined (\u79c1\u6709\u53d8\u91cf)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (IIFE)<\/strong><\/p>\n<ul>\n<li>\u5c06\u51fd\u6570\u4f5c\u4e3a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u4f20\u9012\u7ed9\u7acb\u5373\u8c03\u7528\u7684\u533f\u540d\u51fd\u6570\u3002<\/li>\n<li>\u5728\u533f\u540d\u51fd\u6570\u7684\u5185\u90e8\uff0c\u51fd\u6570\u88ab\u8c03\u7528\u5e76\u8fd4\u56de\u5176\u7ed3\u679c\u3002<\/li>\n<li>\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u79c1\u6709\u4f5c\u7528\u57df\u7684\u5bf9\u8c61\u6216\u95ed\u5305\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>(function() {\n  let count = 0;\n\n  return {\n    increment: function() {\n      count++;\n    },\n    decrement: function() {\n      count--;\n    },\n    getCount: function() {\n      return count;\n    }\n  };\n})();\n\nconst counter = createCounter();\ncounter.increment();\nconsole.log(counter.getCount()); \/\/ 1\nconsole.log(counter.count); \/\/ undefined (\u79c1\u6709\u53d8\u91cf)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5c01\u88c5\u6548\u679c\u3002\u5de5\u5382\u6a21\u5f0f\u901a\u5e38\u4f7f\u7528\u5bf9\u8c61\uff0c\u800c IIFE \u4f7f\u7528\u95ed\u5305\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u504f\u597d\uff0c\u53ef\u4ee5\u9009\u62e9\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684js\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\uff0c\u5c01\u88c5\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u79c1\u6709\u4f5c\u7528\u57df\uff0c\u901a\u8fc7\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\u5b9e\u73b0\uff1a\u5de5\u5382\u6a21\u5f0f\uff1a\u521b\u5efa\u5305\u542b\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u3002\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (iife)\uff1a\u5c06\u51fd\u6570\u4f5c\u4e3a\u7acb\u5373\u8c03\u7528\u7684\u533f\u540d\u51fd\u6570\u7684\u53c2\u6570\uff0c\u51fd\u6570\u5728\u5185\u90e8\u88ab\u8c03\u7528\uff0c\u8fd4\u56de\u5e26\u6709\u79c1\u6709\u4f5c\u7528\u57df\u7684\u5bf9\u8c61\u6216\u95ed\u5305\u3002 \u5982\u4f55\u5c01\u88c5\u81ea\u5df1\u7684 JavaScript \u51fd\u6570 \u5728 JavaScript \u4e2d\u5c01\u88c5\u51fd\u6570\u662f\u521b\u5efa\u4e00\u4e2a\u79c1\u6709\uff0c\u4f7f\u51fd\u6570\u5185\u7684\u53d8\u91cf\u548c\u65b9\u6cd5\u4ec5\u5728\u51fd\u6570\u5185\u90e8\u53ef\u89c1\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5305\u62ec\uff1a \u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\uff1a\u53ef\u4ee5\u9690\u85cf\u51fd\u6570\u7684\u5185\u90e8\u5de5\u4f5c\u539f\u7406\uff0c\u8ba9\u7528\u6237\u53ea\u5173\u6ce8\u516c\u5f00\u7684\u63a5\u53e3\u3002 \u9632\u6b62\u51b2\u7a81\uff1a\u53ef\u4ee5\u9632\u6b62\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u548c\u51fd\u6570\u540d\u79f0\u4e0e\u5176\u4ed6\u4ee3\u7801\u51b2\u7a81\u3002 \u6a21\u5757\u5316\uff1a\u53ef\u4ee5\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u4fbf\u4e8e\u91cd\u7528\u548c\u7ef4\u62a4\u3002 \u5c01\u88c5\u51fd\u6570\u7684\u65b9\u6cd5 \u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u5c01\u88c5 JavaScript \u51fd\u6570\uff1a 1. \u5de5\u5382\u6a21\u5f0f \u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u3002 \u5bf9\u8c61\u7684\u5185\u90e8\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u79c1\u6709\u7684\u3002 \u5bf9\u8c61\u7684\u5916\u90e8\u63a5\u53e3\u662f\u901a\u8fc7\u516c\u5f00\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u8bbf\u95ee\u7684\u3002 \u793a\u4f8b\uff1a function createCounter() { let count = 0; return { increment: function() { count++; }, decrement: function() { count&#8211;; }, getCount: function() { return count; } }; } const counter = createCounter(); counter.increment(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3352","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3352"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3352\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}