{"id":3291,"date":"2024-11-10T13:46:52","date_gmt":"2024-11-10T05:46:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3291\/"},"modified":"2024-11-10T13:46:52","modified_gmt":"2024-11-10T05:46:52","slug":"js%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3291\/","title":{"rendered":"js\u5982\u4f55\u5bfc\u5165\u53d8\u91cf"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u5bfc\u5165\u53d8\u91cf\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6a21\u5757\u7cfb\u7edf\u3002commonjs \u4f7f\u7528 require() \u51fd\u6570\u5bfc\u5165\u53d8\u91cf\uff0c\u800c es modules \u4f7f\u7528 import \u8bed\u53e5\u3002\u53e6\u5916\uff0ces modules \u8fd8\u5141\u8bb8\u901a\u8fc7 export \u8bed\u53e5\u5bfc\u51fa\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5176\u4ed6\u6a21\u5757\u901a\u8fc7 import \u8bed\u53e5\u5bfc\u5165\u4f7f\u7528\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809122571215.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5bfc\u5165\u53d8\u91cf\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5bfc\u5165\u53d8\u91cf\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5bfc\u5165\u53d8\u91cf<\/strong><\/p>\n<p>JavaScript \u4e2d\u5bfc\u5165\u53d8\u91cf\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u6a21\u5757\u7cfb\u7edf\u3002\u6709\u4e24\u79cd\u4e3b\u8981\u6a21\u5757\u7cfb\u7edf\uff1aCommonJS \u548c ES Modules\u3002<\/p>\n<p><strong>CommonJS<\/strong><\/p>\n<p>\u5728 CommonJS \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 require() \u51fd\u6570\u5bfc\u5165\u53d8\u91cf\uff1a<\/p>\n<pre>const myVariable = require('.\/my-module.js');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d my-module.js \u662f\u5305\u542b\u8981\u5bfc\u5165\u7684\u53d8\u91cf\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>ES Modules<\/strong><\/p>\n<p>\u5728 ES Modules \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 import \u8bed\u53e5\u5bfc\u5165\u53d8\u91cf\uff1a<\/p>\n<pre>import { myVariable } from '.\/my-module.js';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d my-module.js \u662f\u5305\u542b\u8981\u5bfc\u5165\u7684\u53d8\u91cf\u7684\u6587\u4ef6\uff0cmyVariable \u662f\u8981\u5bfc\u5165\u7684\u53d8\u91cf\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li> <strong>CommonJS<\/strong> \u6a21\u5757\u7cfb\u7edf\u662f Node.js \u4e2d\u4f7f\u7528\u7684\u4f20\u7edf\u6a21\u5757\u7cfb\u7edf\u3002\u5b83\u4f7f\u7528 require() \u51fd\u6570\u6765\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\u3002<\/li>\n<li> <strong>ES Modules<\/strong> \u662f JavaScript \u7684\u539f\u751f\u6a21\u5757\u7cfb\u7edf\u3002\u5b83\u4f7f\u7528 import \u8bed\u53e5\u6765\u9759\u6001\u52a0\u8f7d\u6a21\u5757\uff0c\u5e76\u4e14\u5177\u6709\u66f4\u597d\u7684\u4ee3\u7801\u7ec4\u7ec7\u6027\u548c\u6a21\u5757\u3002<\/li>\n<li>\u5728 ES Modules \u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 export \u8bed\u53e5\u5c06\u53d8\u91cf\u5bfc\u51fa\u5230\u5176\u4ed6\u6a21\u5757\uff1a<\/li>\n<\/ul>\n<pre>\/\/ my-module.js\nexport const myVariable = 10;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u7136\u540e\uff0c\u5176\u4ed6\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7 import \u8bed\u53e5\u5bfc\u5165\u8be5\u53d8\u91cf\uff1a<\/li>\n<\/ul>\n<pre>\/\/ another-module.js\nimport { myVariable } from '.\/my-module.js';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0canother-module.js \u53ef\u4ee5\u8bbf\u95ee myVariable \u53d8\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5bfc\u5165\u53d8\u91cf\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\u5bfc\u5165\u53d8\u91cf\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6a21\u5757\u7cfb\u7edf\u3002commonjs \u4f7f\u7528 require() \u51fd\u6570\u5bfc\u5165\u53d8\u91cf\uff0c\u800c es modules \u4f7f\u7528 import \u8bed\u53e5\u3002\u53e6\u5916\uff0ces modules \u8fd8\u5141\u8bb8\u901a\u8fc7 export \u8bed\u53e5\u5bfc\u51fa\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5176\u4ed6\u6a21\u5757\u901a\u8fc7 import \u8bed\u53e5\u5bfc\u5165\u4f7f\u7528\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u5bfc\u5165\u53d8\u91cf JavaScript \u4e2d\u5bfc\u5165\u53d8\u91cf\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u6a21\u5757\u7cfb\u7edf\u3002\u6709\u4e24\u79cd\u4e3b\u8981\u6a21\u5757\u7cfb\u7edf\uff1aCommonJS \u548c ES Modules\u3002 CommonJS \u5728 CommonJS \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 require() \u51fd\u6570\u5bfc\u5165\u53d8\u91cf\uff1a const myVariable = require(&#8216;.\/my-module.js&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d my-module.js \u662f\u5305\u542b\u8981\u5bfc\u5165\u7684\u53d8\u91cf\u7684\u6587\u4ef6\u3002 ES Modules \u5728 ES Modules \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 import \u8bed\u53e5\u5bfc\u5165\u53d8\u91cf\uff1a import { myVariable } from &#8216;.\/my-module.js&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d [&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-3291","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3291","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=3291"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3291\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}