{"id":2525,"date":"2024-11-10T17:14:53","date_gmt":"2024-11-10T09:14:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2525\/"},"modified":"2024-11-10T17:14:53","modified_gmt":"2024-11-10T09:14:53","slug":"js-%e9%97%ad%e5%8c%85%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e9%97%ad%e5%8c%85%e7%9a%84%e8%a1%a8%e8%be%be%e5%bc%8f%e6%98%af%e4%b8%a4%e4%b8%aa%e8%bf%9e%e7%bb%ad%e7%9a%84%e6%8b%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2525\/","title":{"rendered":"JS \u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u62ec\u53f7\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173078370446532.jpg\" class=\"aligncenter\" title=\"JS \u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u62ec\u53f7\uff1f\u63d2\u56fe\" alt=\"JS \u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u62ec\u53f7\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js\u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\uff1f<\/strong><\/p>\n<p>\u4f5c\u4e3a\u4ece++\/java\u8f6c\u6765\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u4e86\u89e3js\u7684\u95ed\u5305\u673a\u5236\u53ef\u80fd\u4e0d\u592a\u4e60\u60ef\u3002js\u4e2d\u7684\u95ed\u5305\u8868\u8fbe\u5f0f\u4f7f\u7528\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\uff0c\u4f3c\u4e4e\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u672c\u6587\u5c06\u6df1\u5165\u89e3\u91ca\u95ed\u5305\u7684\u539f\u7406\u548c\u8fd9\u79cd\u8868\u8fbe\u5f62\u5f0f\u7684\u89c4\u8303\u3002<\/p>\n<p><strong>\u95ed\u5305\u7684\u5b9a\u4e49<\/strong><\/p>\n<p>\u95ed\u5305\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5f53\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u4fdd\u7559\u7740\u5bf9\u6267\u884c\u73af\u5883\u7684\u8bbf\u95ee\u3002\u8fd9\u79cd\u6267\u884c\u73af\u5883\u5305\u62ec\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u5916\u90e8\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>\u8868\u8fbe\u5f0f\u7684\u89e3\u6790<\/strong><\/p>\n<p>\u5728js\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u62ec\u53f7\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u58f0\u660e\u5e76\u8c03\u7528\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u53c2\u6570i\u4f20\u7ed9\u51fd\u6570\u4f53\uff1a<\/p>\n<pre>(function(i) {\n  \/\/ \u51fd\u6570\u4f53\n})(0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>function myfunction(i) {\n  \/\/ \u51fd\u6570\u4f53\n}\n\nmyfunction(0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (iife)<\/strong><\/p>\n<p>\u5c06\u51fd\u6570\u7acb\u5373\u8c03\u7528\u7684\u8fd9\u79cd\u6a21\u5f0f\u79f0\u4e3a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(iife)\u3002\u5b83\u5728\u521b\u5efa\u95ed\u5305\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002\u95ed\u5305\u7684\u521b\u5efa\u9700\u8981\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u5f15\u5165\u4e86\u81ea\u5df1\u7684\u4f5c\u7528\u57df<\/li>\n<li>\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<p><strong>\u4f5c\u7528\u57df\u5206\u79bb<\/strong><\/p>\n<p>\u95ed\u5305\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u4ece\u800c\u5c06\u53d8\u91cf\u548c\u51fd\u6570\u9694\u79bb\u5230\u7279\u5b9a\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u5c01\u88c5\u6216\u79c1\u6709\u5316\u6570\u636e\u548c\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u662f\u4e00\u4e2a\u95ed\u5305\u7684\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aa\u7684\u65b0\u4f5c\u7528\u57df\uff1a<\/p>\n<pre>var a = 6;\n(function() {\n  'use strict';\n  var a = 5;\n})();\n\nconsole.log(a); \/\/ \u8f93\u51fa: 6<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5373\u4f7f\u5728\u5185\u90e8\u4f5c\u7528\u57df\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e86a\uff0c\u5916\u90e8\u4f5c\u7528\u57df\u7684a\u4ecd\u7136\u4fdd\u7559\u5176\u539f\u59cb\u503c\u3002<\/p>\n<p><strong>\u89c4\u8303<\/strong><\/p>\n<p>\u8fd9\u79cd\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\u7684\u8868\u8fbe\u5f0f\u89c4\u8303\u662f\u4e3a\u4e86\u786e\u4fdd\u95ed\u5305\u7684\u6b63\u786e\u521b\u5efa\u548c\u6267\u884c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u6e05\u6670\u660e\u786e\u7684\u65b9\u6cd5\u6765\u58f0\u660e\u548c\u8c03\u7528\u95ed\u5305\u51fd\u6570\uff0c\u907f\u514d\u4e86\u6df7\u6dc6\u6216\u610f\u5916\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJS \u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u62ec\u53f7\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\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\uff1f \u4f5c\u4e3a\u4ece++\/java\u8f6c\u6765\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u4e86\u89e3js\u7684\u95ed\u5305\u673a\u5236\u53ef\u80fd\u4e0d\u592a\u4e60\u60ef\u3002js\u4e2d\u7684\u95ed\u5305\u8868\u8fbe\u5f0f\u4f7f\u7528\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\uff0c\u4f3c\u4e4e\u8ba9\u4eba\u6478\u4e0d\u7740\u5934\u8111\u3002\u672c\u6587\u5c06\u6df1\u5165\u89e3\u91ca\u95ed\u5305\u7684\u539f\u7406\u548c\u8fd9\u79cd\u8868\u8fbe\u5f62\u5f0f\u7684\u89c4\u8303\u3002 \u95ed\u5305\u7684\u5b9a\u4e49 \u95ed\u5305\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5f53\u6267\u884c\u5b8c\u6bd5\u540e\u4ecd\u7136\u4fdd\u7559\u7740\u5bf9\u6267\u884c\u73af\u5883\u7684\u8bbf\u95ee\u3002\u8fd9\u79cd\u6267\u884c\u73af\u5883\u5305\u62ec\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u5916\u90e8\u4e2d\u53ef\u89c1\u7684\u53d8\u91cf\u3002 \u8868\u8fbe\u5f0f\u7684\u89e3\u6790 \u5728js\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u62ec\u53f7\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u7acb\u5373\u8c03\u7528\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u58f0\u660e\u5e76\u8c03\u7528\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u53c2\u6570i\u4f20\u7ed9\u51fd\u6570\u4f53\uff1a (function(i) { \/\/ \u51fd\u6570\u4f53 })(0); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u4ee3\u7801\uff1a function myfunction(i) { \/\/ \u51fd\u6570\u4f53 } myfunction(0); \u767b\u5f55\u540e\u590d\u5236 \u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (iife) \u5c06\u51fd\u6570\u7acb\u5373\u8c03\u7528\u7684\u8fd9\u79cd\u6a21\u5f0f\u79f0\u4e3a\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f(iife)\u3002\u5b83\u5728\u521b\u5efa\u95ed\u5305\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002\u95ed\u5305\u7684\u521b\u5efa\u9700\u8981\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a \u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u5f15\u5165\u4e86\u81ea\u5df1\u7684\u4f5c\u7528\u57df \u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf \u4f5c\u7528\u57df\u5206\u79bb \u95ed\u5305\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u4ece\u800c\u5c06\u53d8\u91cf\u548c\u51fd\u6570\u9694\u79bb\u5230\u7279\u5b9a\u4f5c\u7528\u57df\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u5c01\u88c5\u6216\u79c1\u6709\u5316\u6570\u636e\u548c\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002 \u793a\u4f8b \u4ee5\u4e0b\u4ee3\u7801\u662f\u4e00\u4e2a\u95ed\u5305\u7684\u793a\u4f8b\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aa\u7684\u65b0\u4f5c\u7528\u57df\uff1a var a = 6; (function() { &#8216;use strict&#8217;; var a = 5; })(); console.log(a); \/\/ \u8f93\u51fa: 6 \u767b\u5f55\u540e\u590d\u5236 \u5373\u4f7f\u5728\u5185\u90e8\u4f5c\u7528\u57df\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e86a\uff0c\u5916\u90e8\u4f5c\u7528\u57df\u7684a\u4ecd\u7136\u4fdd\u7559\u5176\u539f\u59cb\u503c\u3002 \u89c4\u8303 \u8fd9\u79cd\u8fde\u7eed\u4e24\u4e2a\u62ec\u53f7\u7684\u8868\u8fbe\u5f0f\u89c4\u8303\u662f\u4e3a\u4e86\u786e\u4fdd\u95ed\u5305\u7684\u6b63\u786e\u521b\u5efa\u548c\u6267\u884c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u6e05\u6670\u660e\u786e\u7684\u65b9\u6cd5\u6765\u58f0\u660e\u548c\u8c03\u7528\u95ed\u5305\u51fd\u6570\uff0c\u907f\u514d\u4e86\u6df7\u6dc6\u6216\u610f\u5916\u6027\u3002 \u4ee5\u4e0a\u5c31\u662fJS \u95ed\u5305\u8be6\u89e3\uff1a\u4e3a\u4ec0\u4e48\u95ed\u5305\u7684\u8868\u8fbe\u5f0f\u662f\u4e24\u4e2a\u8fde\u7eed\u7684\u62ec\u53f7\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-2525","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2525","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=2525"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2525\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}