{"id":269,"date":"2021-06-29T20:30:01","date_gmt":"2021-06-29T12:30:01","guid":{"rendered":"http:\/\/gjweb.top\/?p=269"},"modified":"2021-06-29T20:30:02","modified_gmt":"2021-06-29T12:30:02","slug":"js%e6%a8%a1%e5%9d%97%e5%8c%96%e7%9a%84%e5%87%a0%e7%a7%8d%e8%a7%84%e8%8c%83","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=269","title":{"rendered":"JS\u6a21\u5757\u5316\u7684\u51e0\u79cd\u89c4\u8303"},"content":{"rendered":"\n<p>\u4e00\u4e2a\u6a21\u5757\u662f\u80fd\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u6587\u4ef6\uff0c\u6709\u4e86\u6a21\u5757\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u4f7f\u7528\u522b\u4eba\u7684\u4ee3\u7801\uff0c\u60f3\u8981\u4ec0\u4e48\u529f\u80fd\u52a0\u8f7d\u4ec0\u4e48\u6a21\u5757<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">common JS \u6a21\u5757\u5316<\/h2>\n\n\n\n<p>\u5f00\u59cb\u4e8e\u670d\u52a1\u7aef\u7684\u6a21\u5757\u5316\uff0c \u540c\u6b65\u5b9a\u4e49\u7684\u6a21\u5757\u5316\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u5355\u72ec\u7684\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u8f93\u51fa<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const xxx = require(xxx)\r\n\/\/ \u7b2c\u4e09\u65b9\u6a21\u5757\uff1a \u53c2\u6570\u4e3a\u6a21\u5757\u540d\uff08\u5305\u540d\uff09\r\n\/\/ \u81ea\u5b9a\u4e49\u6a21\u5757 \u53c2\u6570\u4e3a\u8def\u5f84<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5f15\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00\r\nexports.xxx = any\r\n\/\/ \u65b9\u5f0f\u4e8c\r\nmodule.exports = any<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d4f\u89c8\u5668\u6253\u5305\u5de5\u5177 browserify<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">commonJS\u7684\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f18\u70b9<ul><li>\u670d\u52a1\u70b9\u6a21\u5757\u91cd\u7528<\/li><li>NPM\u4e2d\u6a21\u5757\u4e30\u5bcc<\/li><\/ul><\/li><li>\u7f3a\u70b9<ul><li>\u52a0\u8f7d\u6a21\u5757\u662f\u540c\u6b65\uff08\u963b\u585e\u52a0\u8f7d\uff09\u7684\uff0c\u53ea\u6709\u52a0\u8f7d\u5b8c\u6210\u540e\u624d\u80fd\u6267\u884c\u540e\u9762\u7684\u64cd\u4f5c<\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">AMD\u6a21\u5757\u5316<\/h2>\n\n\n\n<p>\u9274\u4e8e\u6d4f\u89c8\u5668\u7684\u7279\u6b8a\u60c5\u51b5\uff0cAMD\u6a21\u5757\u5316\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u52a0\u8f7d\u4f9d\u8d56\u6a21\u5757\uff0c\u4e13\u7528\u4e8e\u6d4f\u89c8\u5668\u7aef<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u8f93\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6ca1\u6709\u4f9d\u8d56\u7684\u6a21\u5757\r\ndefine(function(){return \u6a21\u5757})\r\n\/\/ \u5b9a\u4e49\u6709\u4f9d\u8d56\u7684\u6a21\u5757\r\ndefine(&#91;'module','module1'],function(m1,m2){\r\n    return \u6a21\u5757\r\n})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5f15\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>require(&#91;'module','module'],function(m1,m2){})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6587\u4ef6\u914d\u7f6e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>require.fonction({\r\n    paths:{\r\n        \u6a21\u5757\u540d:\"\u6a21\u5757\u8def\u5f84\"\r\n    }\r\n})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6253\u5305\u5de5\u5177 Require.js<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">AMD\u7684\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f18\u70b9<ul><li>\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u5f02\u6b65\u52a0\u8f7d\u6a21\u5757<\/li><li>\u5e76\u884c\u52a0\u8f7d\u591a\u4e86\u6a21\u5757<\/li><\/ul><\/li><li>\u7f3a\u70b9<ul><li>\u5f00\u53d1\u6210\u672c\u9ad8\uff0c\u4ee3\u7801\u7684\u9605\u8bfb\u548c\u4e66\u5199\u6bd4\u8f83\u56f0\u96be<\/li><li>\u6a21\u5757\u5b9a\u4e49\u65b9\u5f0f\u7684\u8bed\u4e49\u4e0d\u901a\u987a<\/li><li>\u4e0d\u7b26\u5408\u901a\u7528\u7684\u6a21\u5757\u5316\u601d\u7ef4\u65b9\u5f0f<\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">CMD<\/h3>\n\n\n\n<p>CMD\u89c4\u8303\u548c AMD \u5f88\u76f8\u4f3c\uff0c\u5c3d\u91cf\u4fdd\u6301\u7b80\u5355\uff0c\u5e76\u4e0e CommonJS \u548c Node.js \u7684 Modules \u89c4\u8303\u4fdd\u6301\u4e86\u5f88\u5927\u7684\u517c\u5bb9\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u8f93\u51fa<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5b9a\u4e49\u6a21\u5757\r\ndefine(function(require,export,module){\r\n    \/\/ \u5f15\u5165\u4f9d\u8d56\uff08\u540c\u6b65\uff09\r\n    var xxx = require(\"\u4f9d\u8d56\u6a21\u5757\u8def\u5f84\")\r\n    \/\/ \u5f15\u5165\u4f9d\u8d56\uff08\u5f02\u6b65\uff09\r\n    require.async(\"\u8def\u5f84\",function(m3){})\r\n    exports.xxx = value\r\n})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5f15\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>define(function(require){\r\n    var m1 = require('\u6a21\u5757\u8def\u5f84')\r\n    var m2 = require(\"\u8def\u5f84\")\r\n    m1()\r\n    m2()\r\n})<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6253\u5305\u5de5\u5177 sea.js<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">CMD\u7684\u4f18\u7f3a\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f18\u70b9<ul><li>\u4f9d\u8d56\u5c31\u8fd1\uff0c\u5ef6\u8fdf\u6267\u884c \u53ef\u4ee5\u5f88\u5bb9\u6613\u5728Node.js\u4e2d\u8fd0\u884c<\/li><\/ul><\/li><li>\u7f3a\u70b9<ul><li>\u4f9d\u8d56SPM\u6253\u5305\uff0c\u6a21\u5757\u7684\u52a0\u8f7d\u903b\u8f91\u504f\u91cd<\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">ES6\u6a21\u5757\u89c4\u8303<\/h2>\n\n\n\n<p>\u5728ES6\u4e4b\u524d, JS\u6ca1\u6709\u63d0\u4f9b\u5b98\u65b9\u7684\u6a21\u5757\u5316, CommonJs \u548c AMD \u4e24\u79cd\u6a21\u5757\u5316\u89c4\u8303\u8f83\u4e3a\u6d3b\u8dc3, \u524d\u8005\u7528\u4e8e\u670d\u52a1\u7aef,\u540e\u8005\u7528\u4e8e\u6d4f\u89c8\u5668. ES6 \u5728\u8bed\u8a00\u6807\u51c6\u7684\u5c42\u9762\u4e0a\uff0c\u5b9e\u73b0\u4e86\u6a21\u5757\u529f\u80fd\uff0c\u800c\u4e14\u5b9e\u73b0\u5f97\u76f8\u5f53\u7b80\u5355\uff0c\u5b8c\u5168\u53ef\u4ee5\u53d6\u4ee3 CommonJS \u548c AMD \u89c4\u8303\uff0c\u6210\u4e3a\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u901a\u7528\u7684\u6a21\u5757\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u8f93\u51fa<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00 \u7edf\u4e00\u8f93\u51fa\r\nfunction a (){}\r\nfunction b (){}\r\nexport {a,b}\r\n\/\/ \u65b9\u5f0f\u4e8c \u9010\u4e2a\u8f93\u51fa\r\nexport function xxx(){}\r\nexport var a = &#91;]\r\n\/\/ \u65b9\u5f0f\u4e09 \u9ed8\u8ba4\u66b4\u9732\r\nexport default value<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5f15\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00\u548c\u65b9\u5f0f\u4e8c \u5f15\u5165\u65b9\u5f0f\r\nimport {a , b} from \"\u8def\u5f84\"\r\n\u200b\r\n\/\/ \u65b9\u5f0f\u4e09 \u5f15\u5165\u65b9\u5f0f\r\nimport xxx from \"\u8def\u5f84\"\r\n\u200b\r\n\/\/ \u7b2c\u4e09\u65b9\u5f15\u5165\r\nimport $ from \"jquery\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6253\u5305\u5de5\u5177<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Babel \u5c06 ES6 \u7f16\u8bd1\u6210 ES5\r\n\u6216\u8005\r\nBrowserify \u7f16\u8bd1\u6253\u5305<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u6a21\u5757\u662f\u80fd\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u6587\u4ef6\uff0c\u6709\u4e86\u6a21\u5757\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u4f7f\u7528\u522b\u4eba\u7684\u4ee3\u7801\uff0c\u60f3\u8981\u4ec0\u4e48\u529f\u80fd\u52a0\u8f7d\u4ec0\u4e48\u6a21\u5757 common J [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}