{"id":2458,"date":"2024-11-10T10:24:55","date_gmt":"2024-11-10T02:24:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2458\/"},"modified":"2024-11-10T10:24:55","modified_gmt":"2024-11-10T02:24:55","slug":"javascript-%e9%97%ad%e5%8c%85%e7%9a%84%e5%8f%8c%e6%8b%ac%e5%8f%b7%ef%bc%8c%e7%a9%b6%e7%ab%9f%e6%98%af%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%91%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2458\/","title":{"rendered":"JavaScript \u95ed\u5305\u7684\u53cc\u62ec\u53f7()()\uff0c\u7a76\u7adf\u662f\u600e\u6837\u5b9e\u73b0\u7684\u5462\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173076985172564.jpg\" class=\"aligncenter\" title=\"JavaScript \u95ed\u5305\u7684\u53cc\u62ec\u53f7()()\uff0c\u7a76\u7adf\u662f\u600e\u6837\u5b9e\u73b0\u7684\u5462\uff1f\u63d2\u56fe\" alt=\"JavaScript \u95ed\u5305\u7684\u53cc\u62ec\u53f7()()\uff0c\u7a76\u7adf\u662f\u600e\u6837\u5b9e\u73b0\u7684\u5462\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>JS\u4e2d\u7684\u95ed\u5305\u7591\u95ee\u89e3\u6790<\/strong><\/p>\n<p>\u4f5c\u4e3a\u4e00\u540d\u4eceC++\/Java\u8f6c\u5b66JS\u7684\u5f00\u53d1\u8005\uff0c\u4f60\u53ef\u80fd\u4f1a\u5bf9\u95ed\u5305\u8bed\u6cd5\u4ea7\u751f\u7591\u60d1\uff0c\u7279\u522b\u662f\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7()()\u7684\u4f7f\u7528\u3002\u672c\u6587\u5c06\u6df1\u5165\u89e3\u6790\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3\u95ed\u5305\u7684\u539f\u7406\u548c\u89c4\u8303\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u53cc\u62ec\u53f7()()\uff1f<\/strong><\/p>\n<p>\u5728JS\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u540e\u9762\u52a0\u4e0a\u5706\u62ec\u5f27\u8868\u793a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c(0)\u8c03\u7528\u4e86\u8fed\u4ee3\u5668\u51fd\u6570\uff0c\u5c06\u53c2\u65700\u4f20\u9012\u7ed9i\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5f53\u4f60\u5c06\u4e00\u4e2a\u51fd\u6570\u58f0\u660e\u653e\u5728\u5706\u62ec\u5f27\u5185\u65f6\uff0c\u5373\u521b\u5efa\u4e86\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u3002\u4f8b\u5982\uff0c(function() {})(0);\u4f1a\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u5185\u90e8\u4ee3\u7801\u3002<\/p>\n<p><strong>\u95ed\u5305\u7684<\/strong><\/p>\n<p>\u95ed\u5305\u662f\u4e00\u4e2a\u4fdd\u7559\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u8bbf\u95ee\u6743\u9650\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<p><strong>\u95ed\u5305\u7684\u8bed\u6cd5\u89c4\u8303<\/strong><\/p>\n<p>\u95ed\u5305\u4e00\u822c\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u8bed\u6cd5\u65b9\u5f0f\u521b\u5efa\uff1a<\/p>\n<ul>\n<li> <strong>\u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a<\/strong> (function() {})(0);<\/li>\n<li> <strong>\u5177\u540d\u51fd\u6570\u8c03\u7528\uff1a<\/strong> (function foo() {})(0);<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2a\u8bed\u6cd5\u5728\u529f\u80fd\u4e0a\u662f\u7b49\u6548\u7684\uff0c\u90fd\u662f\u901a\u8fc7\u7acb\u5373\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u521b\u5efa\u95ed\u5305\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u662f\u8fd9\u4e2a\u89c4\u8303\uff1f<\/strong><\/p>\n<p>\u95ed\u5305\u7684\u53cc\u62ec\u53f7\u8bed\u6cd5\u6e90\u81eaJavaScript\u65e9\u671f\u7684\u8bbe\u8ba1\u89c4\u8303\u3002\u5f53\u65f6\uff0cJavaScript\u8fd8\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u800c\u95ed\u5305\u9700\u8981\u4e00\u4e2a\u5355\u72ec\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u53cc\u62ec\u53f7()()\u6765\u660e\u786e\u533a\u5206\u51fd\u6570\u8c03\u7528\u548c\u95ed\u5305\u521b\u5efa\u3002<\/p>\n<p><strong>\u7406\u89e3\u95ed\u5305<\/strong><\/p>\n<p>\u603b\u4e4b\uff0c\u95ed\u5305\u662f:( )()\u6784\u9020\u4e2d\u521b\u5efa\u7684\u4e00\u4e2a\u4fdd\u7559\u5916\u90e8\u51fd\u6570\u53d8\u91cf\u8bbf\u95ee\u6743\u9650\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u8bed\u6cd5\u89c4\u8303\u662f\u4e3a\u4e86\u5728JavaScript\u65e9\u671f\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u65f6\u63d0\u4f9b\u95ed\u5305\u529f\u80fd\u800c\u8bbe\u8ba1\u7684\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u95ed\u5305\u7684\u53cc\u62ec\u53f7()()\uff0c\u7a76\u7adf\u662f\u600e\u6837\u5b9e\u73b0\u7684\u5462\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\u4e2d\u7684\u95ed\u5305\u7591\u95ee\u89e3\u6790 \u4f5c\u4e3a\u4e00\u540d\u4eceC++\/Java\u8f6c\u5b66JS\u7684\u5f00\u53d1\u8005\uff0c\u4f60\u53ef\u80fd\u4f1a\u5bf9\u95ed\u5305\u8bed\u6cd5\u4ea7\u751f\u7591\u60d1\uff0c\u7279\u522b\u662f\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7()()\u7684\u4f7f\u7528\u3002\u672c\u6587\u5c06\u6df1\u5165\u89e3\u6790\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3\u95ed\u5305\u7684\u539f\u7406\u548c\u89c4\u8303\u3002 \u4e3a\u4ec0\u4e48\u4f7f\u7528\u53cc\u62ec\u53f7()()\uff1f \u5728JS\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u540e\u9762\u52a0\u4e0a\u5706\u62ec\u5f27\u8868\u793a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c(0)\u8c03\u7528\u4e86\u8fed\u4ee3\u5668\u51fd\u6570\uff0c\u5c06\u53c2\u65700\u4f20\u9012\u7ed9i\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f \u5f53\u4f60\u5c06\u4e00\u4e2a\u51fd\u6570\u58f0\u660e\u653e\u5728\u5706\u62ec\u5f27\u5185\u65f6\uff0c\u5373\u521b\u5efa\u4e86\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(IIFE)\u3002\u4f8b\u5982\uff0c(function() {})(0);\u4f1a\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u8fd0\u884c\u5176\u5185\u90e8\u4ee3\u7801\u3002 \u95ed\u5305\u7684 \u95ed\u5305\u662f\u4e00\u4e2a\u4fdd\u7559\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u8bbf\u95ee\u6743\u9650\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u53ef\u4ee5\u5728\u5916\u90e8\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002 \u95ed\u5305\u7684\u8bed\u6cd5\u89c4\u8303 \u95ed\u5305\u4e00\u822c\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u8bed\u6cd5\u65b9\u5f0f\u521b\u5efa\uff1a \u533f\u540d\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a (function() {})(0); \u5177\u540d\u51fd\u6570\u8c03\u7528\uff1a (function foo() {})(0); \u8fd9\u4e24\u4e2a\u8bed\u6cd5\u5728\u529f\u80fd\u4e0a\u662f\u7b49\u6548\u7684\uff0c\u90fd\u662f\u901a\u8fc7\u7acb\u5373\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u521b\u5efa\u95ed\u5305\u3002 \u4e3a\u4ec0\u4e48\u662f\u8fd9\u4e2a\u89c4\u8303\uff1f \u95ed\u5305\u7684\u53cc\u62ec\u53f7\u8bed\u6cd5\u6e90\u81eaJavaScript\u65e9\u671f\u7684\u8bbe\u8ba1\u89c4\u8303\u3002\u5f53\u65f6\uff0cJavaScript\u8fd8\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u800c\u95ed\u5305\u9700\u8981\u4e00\u4e2a\u5355\u72ec\u7684\u4f5c\u7528\u57df\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u53cc\u62ec\u53f7()()\u6765\u660e\u786e\u533a\u5206\u51fd\u6570\u8c03\u7528\u548c\u95ed\u5305\u521b\u5efa\u3002 \u7406\u89e3\u95ed\u5305 \u603b\u4e4b\uff0c\u95ed\u5305\u662f:( )()\u6784\u9020\u4e2d\u521b\u5efa\u7684\u4e00\u4e2a\u4fdd\u7559\u5916\u90e8\u51fd\u6570\u53d8\u91cf\u8bbf\u95ee\u6743\u9650\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u8bed\u6cd5\u89c4\u8303\u662f\u4e3a\u4e86\u5728JavaScript\u65e9\u671f\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u65f6\u63d0\u4f9b\u95ed\u5305\u529f\u80fd\u800c\u8bbe\u8ba1\u7684\u3002 \u4ee5\u4e0a\u5c31\u662fJavaScript \u95ed\u5305\u7684\u53cc\u62ec\u53f7()()\uff0c\u7a76\u7adf\u662f\u600e\u6837\u5b9e\u73b0\u7684\u5462\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2458","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2458","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=2458"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2458\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}