{"id":2450,"date":"2024-11-10T17:33:10","date_gmt":"2024-11-10T09:33:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2450\/"},"modified":"2024-11-10T17:33:10","modified_gmt":"2024-11-10T09:33:10","slug":"javascript-%e9%97%ad%e5%8c%85%ef%bc%9a%e7%ab%8b%e5%8d%b3%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0%e8%a1%a8%e8%be%be%e5%bc%8fiife%e7%9a%84%e4%bd%9c%e7%94%a8%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2450\/","title":{"rendered":"JavaScript \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173076877595825.jpg\" class=\"aligncenter\" title=\"JavaScript \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"JavaScript \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u4ece\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u8f6c\u5230 javascript \u65f6\uff0c\u95ed\u5305\u7684\u6982\u5ff5\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\u3002\u672c\u95ee\u7b54\u5c06\u63ed\u5f00\u95ed\u5305\u7684\u8c1c\u56e2\uff0c\u5e2e\u52a9\u60a8\u7406\u89e3\u90a3\u4e9b\u5305\u542b\u53cc\u91cd\u62ec\u53f7\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u540d\u79f0\u540e\u9762\u6dfb\u52a0\u5706\u62ec\u53f7\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u79f0\u4e3a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff08iife\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>(function() {\n  console.log(\"hello, world!\");\n})();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u7acb\u5373\u8c03\u7528\u533f\u540d\u51fd\u6570\uff0c\u8f93\u51fa &#8220;hello, world!&#8221;\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>iife \u7684\u4f5c\u7528\u4e4b\u4e00\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5b83\u5185\u90e8\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u4f5c\u7528\u57df\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>var a = 6;\n\n(function() {\n  var a = 5;\n  console.log(a); \/\/ \u8f93\u51fa 5\n})();\n\nconsole.log(a); \/\/ \u8f93\u51fa 6<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>iife \u521b\u9020\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u5176\u4e2d a \u88ab\u91cd\u65b0\u58f0\u660e\u4e3a 5\u3002\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684 a \u4ecd\u7136\u4fdd\u6301\u4e3a 6\u3002<\/p>\n<p>\u95ed\u5305\u7ecf\u5e38\u4f7f\u7528 iife \u6765\u4fdd\u5b58\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u95ed\u5305\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u5143\u7d20\u6570\u7ec4\uff1a<\/p>\n<pre>const elements = [1, 2, 3];\n\nconst iterator = (function() {\n  let i = 0;\n\n  return function() {\n    if (i &lt; elements.length) {\n      return elements[i++];\n    } else {\n      return null;\n    }\n  };\n})();\n\nconsole.log(iterator()); \/\/ \u8f93\u51fa 1\nconsole.log(iterator()); \/\/ \u8f93\u51fa 2\nconsole.log(iterator()); \/\/ \u8f93\u51fa 3\nconsole.log(iterator()); \/\/ \u8f93\u51fa null<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53cc\u91cd\u62ec\u53f7 ()() \u7684\u7ec4\u5408\u5141\u8bb8\u7acb\u5373\u521b\u5efa\u548c\u8c03\u7528\u51fd\u6570\u3002iife \u4f7f\u6211\u4eec\u80fd\u591f\u63a7\u5236\u4f5c\u7528\u57df\u548c\u4fdd\u5b58\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u4ece\u800c\u6269\u5c55\u4e86 javascript \u7684\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\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>js \u95ed\u5305\uff1a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f \u4ece\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u8f6c\u5230 javascript \u65f6\uff0c\u95ed\u5305\u7684\u6982\u5ff5\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\u3002\u672c\u95ee\u7b54\u5c06\u63ed\u5f00\u95ed\u5305\u7684\u8c1c\u56e2\uff0c\u5e2e\u52a9\u60a8\u7406\u89e3\u90a3\u4e9b\u5305\u542b\u53cc\u91cd\u62ec\u53f7\u7684\u8868\u8fbe\u5f0f\u3002 \u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u540d\u79f0\u540e\u9762\u6dfb\u52a0\u5706\u62ec\u53f7\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u79f0\u4e3a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff08iife\uff09\u3002\u4f8b\u5982\uff1a (function() { console.log(&#8220;hello, world!&#8221;); })(); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u9762\u7684\u4ee3\u7801\u7acb\u5373\u8c03\u7528\u533f\u540d\u51fd\u6570\uff0c\u8f93\u51fa &#8220;hello, world!&#8221;\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b iife \u7684\u4f5c\u7528\u4e4b\u4e00\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5b83\u5185\u90e8\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u4f5c\u7528\u57df\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u4f8b\u5982\uff1a var a = 6; (function() { var a = 5; console.log(a); \/\/ \u8f93\u51fa 5 })(); console.log(a); \/\/ \u8f93\u51fa 6 \u767b\u5f55\u540e\u590d\u5236 iife \u521b\u9020\u4e86\u4e00\u4e2a\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u5176\u4e2d a \u88ab\u91cd\u65b0\u58f0\u660e\u4e3a 5\u3002\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684 a \u4ecd\u7136\u4fdd\u6301\u4e3a 6\u3002 \u95ed\u5305\u7ecf\u5e38\u4f7f\u7528 iife \u6765\u4fdd\u5b58\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u95ed\u5305\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u5143\u7d20\u6570\u7ec4\uff1a const elements = [1, [&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-2450","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2450","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=2450"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2450\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}