{"id":10065,"date":"2023-11-28T21:25:44","date_gmt":"2023-11-28T13:25:44","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10065"},"modified":"2023-11-30T19:12:59","modified_gmt":"2023-11-30T11:12:59","slug":"javascript-zhong-mo-kuai-hua-kai-fa-zen-me-zuo","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/javascript-zhong-mo-kuai-hua-kai-fa-zen-me-zuo\/","title":{"rendered":"JavaScript \u4e2d\u6a21\u5757\u5316\u5f00\u53d1\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"<p>JavaScript \u4e2d\u7684\u6a21\u5757\u5316\u5f00\u53d1\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n<h3><a id=\"1-commonjs%EF%BC%88node-js%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. CommonJS\uff08Node.js\uff09\uff1a<\/h3>\n<p>\u5728 Node.js \u4e2d\uff0c\u4f7f\u7528 CommonJS \u89c4\u8303\u8fdb\u884c\u6a21\u5757\u5316\u5f00\u53d1\u3002\u901a\u8fc7 <code>require<\/code> \u5bfc\u5165\u6a21\u5757\uff0c\u901a\u8fc7 <code>module.exports<\/code> \u5bfc\u51fa\u6a21\u5757\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u6a21\u5757\u5bfc\u51fa\n\/\/ example.js\nconst exampleFunction = () =&gt; {\n  \/\/ some code\n};\n\nmodule.exports = exampleFunction;\n\n\/\/ \u6a21\u5757\u5bfc\u5165\n\/\/ index.js\nconst exampleFunction = require('.\/example');\nexampleFunction();\n<\/code><\/pre>\n<h3><a id=\"2-es6-modules%EF%BC%88esm%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. ES6 Modules\uff08ESM\uff09\uff1a<\/h3>\n<p>ES6 \u5f15\u5165\u4e86\u6a21\u5757\u5316\u7684\u8bed\u6cd5\uff0c\u901a\u8fc7 <code>import<\/code> \u5bfc\u5165\u6a21\u5757\uff0c\u901a\u8fc7 <code>export<\/code> \u5bfc\u51fa\u6a21\u5757\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u6a21\u5757\u5bfc\u51fa\n\/\/ example.js\nconst exampleFunction = () =&gt; {\n  \/\/ some code\n};\n\nexport default exampleFunction;\n\n\/\/ \u6a21\u5757\u5bfc\u5165\n\/\/ index.js\nimport exampleFunction from '.\/example.js';\nexampleFunction();\n<\/code><\/pre>\n<h3><a id=\"3-amd%EF%BC%88asynchronous-module-definition%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. AMD\uff08Asynchronous Module Definition\uff09\uff1a<\/h3>\n<p>AMD \u662f\u4e00\u79cd\u5f02\u6b65\u52a0\u8f7d\u6a21\u5757\u7684\u89c4\u8303\uff0c\u4e3b\u8981\u7528\u4e8e\u6d4f\u89c8\u5668\u73af\u5883\uff0c\u4f7f\u7528 <code>define<\/code> \u5b9a\u4e49\u6a21\u5757\uff0c\u4f7f\u7528 <code>require<\/code> \u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u6a21\u5757\u5b9a\u4e49\n\/\/ example.js\ndefine([], function() {\n  return {\n    exampleFunction: function() {\n      \/\/ some code\n    }\n  };\n});\n\n\/\/ \u6a21\u5757\u52a0\u8f7d\n\/\/ index.js\nrequire(['example'], function(exampleModule) {\n  exampleModule.exampleFunction();\n});\n<\/code><\/pre>\n<h3><a id=\"4-umd%EF%BC%88universal-module-definition%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>4. UMD\uff08Universal Module Definition\uff09\uff1a<\/h3>\n<p>UMD \u662f\u4e00\u79cd\u901a\u7528\u6a21\u5757\u5b9a\u4e49\u89c4\u8303\uff0c\u517c\u5bb9 CommonJS\u3001AMD \u548c\u5168\u5c40\u53d8\u91cf\u5bfc\u51fa\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"language-javascript\">(function(root, factory) {\n  if (typeof define === 'function' &amp;&amp; define.amd) {\n    define([], factory);\n  } else if (typeof exports === 'object') {\n    module.exports = factory();\n  } else {\n    root.exampleModule = factory();\n  }\n})(typeof self !== 'undefined' ? self : this, function() {\n  return {\n    exampleFunction: function() {\n      \/\/ some code\n    }\n  };\n});\n<\/code><\/pre>\n<p>\u9009\u62e9\u4f55\u79cd\u6a21\u5757\u5316\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u3001\u6240\u4f7f\u7528\u7684\u73af\u5883\u4ee5\u53ca\u4e2a\u4eba\u6216\u56e2\u961f\u7684\u504f\u597d\u3002ES6 Modules \u5728\u73b0\u4ee3 JavaScript \u4e2d\u5f97\u5230\u5e7f\u6cdb\u652f\u6301\uff0c\u800c CommonJS \u4e3b\u8981\u7528\u4e8e Node.js\uff0c\u800c AMD \u548c UMD \u9010\u6e10\u88ab\u65b0\u7684\u6a21\u5757\u5316\u6807\u51c6\u6240\u66ff\u4ee3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u7684\u6a21\u5757\u5316\u5f00\u53d1\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff1a 1. CommonJS\uff08Node.js\uff09\uff1a  [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[],"class_list":["post-10065","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10065"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10065\/revisions"}],"predecessor-version":[{"id":13678,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10065\/revisions\/13678"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10065"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}