前端模块化的全面总结

<div class="rich_media_content js_underline_content " id="js_content" style="visibility: visible;"><p style="visibility: visible;"><br style="visibility: visible;"></p><p style="box-sizing: border-box; line-height: inherit; margin-top: 22px; margin-bottom: 22px; font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; text-align: start; white-space: normal; background-color: rgb(255, 255, 255); visibility: visible;"><img class="rich_pages wxw-img" data-ratio="0.5625" data-src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/EtoFrfgkjD0qWTJNsLAwu2s8EBDvyyD11KJCWKUa1xbcqANPgN8BDpLxMd4o90avZIyMughM0TmRkm4LBHHiacw/640?wx_fmt=jpeg" data-type="jpeg" data-w="1920" style="box-sizing: border-box; border-style: none; width: 677px !important; height: auto !important; visibility: visible !important;" data-index="1" src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/EtoFrfgkjD0qWTJNsLAwu2s8EBDvyyD11KJCWKUa1xbcqANPgN8BDpLxMd4o90avZIyMughM0TmRkm4LBHHiacw/640?wx_fmt=jpeg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" _width="677px" crossorigin="anonymous" alt="图片" data-fail="0"></p><h2 style="box-sizing: border-box; line-height: 1.5; margin-top: 35px; margin-bottom: 10px; padding-bottom: 12px; font-size: 20px; border-bottom: 1px solid rgb(236, 236, 236); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; text-align: start; white-space: normal; background-color: rgb(255, 255, 255); visibility: visible;">背景</h2><p style="box-sizing: border-box; line-height: inherit; margin-top: 22px; margin-bottom: 22px; font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 16px; text-align: start; white-space: normal; background-color: rgb(255, 255, 255); visibility: visible;">随着前端功能越来越复杂,前端代码日益膨胀,为了减少维护成本,提高代码的可复用性,前端模块化势在必行。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">所有js文件都在一个html中引入,造成以下不良影响:</p><ol style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p><span style="box-sizing: border-box;font-weight: bolder;">请求过多</span>。首先我们要依赖多个模块,那样就会发送多个请求,导致请求过多</p></li><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p><span style="box-sizing: border-box;font-weight: bolder;">依赖模糊</span>。我们不知道他们的具体依赖关系是什么,也就是说很容易因为不了解他们之间的依赖关系导致加载先后顺序出错。</p></li><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p><span style="box-sizing: border-box;font-weight: bolder;">难以维护</span>。以上两种原因就导致了很难维护,很可能出现牵一发而动全身的情况导致项目出现严重的问题。</p></li></ol><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块的概念</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">webpack中是这样定义的:</p><blockquote style="box-sizing: border-box;color: rgb(102, 102, 102);padding: 1px 23px;margin-top: 22px;margin-bottom: 22px;border-left-width: 4px;border-left-color: rgb(203, 203, 203);background-color: rgb(248, 248, 248);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 10px;margin-bottom: 10px;">在模块化编程中,开发者将程序分解成离散功能块(discrete chunks of functionality),并称之为模块。每个模块具有比完整程序更小的接触面,使得校验、调试、测试轻而易举。精心编写的模块提供了可靠的抽象和封装界限,使得应用程序中每个模块都具有条理清楚的设计和明确的目的。</p></blockquote><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块应该是职责单一、相互独立、低耦合的、高度内聚且可替换的离散功能块。</p><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块化的概念</h2><blockquote style="box-sizing: border-box;color: rgb(102, 102, 102);padding: 1px 23px;margin-top: 22px;margin-bottom: 22px;border-left-width: 4px;border-left-color: rgb(203, 203, 203);background-color: rgb(248, 248, 248);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 10px;margin-bottom: 10px;">模块化是一种处理复杂系统分解成为更好的可管理模块的方式,它可以把系统代码划分为一系列职责单一,高度解耦且可替换的模块,系统中某一部分的变化将如何影响其它部分就会变得显而易见,系统的可维护性更加简单易得。</p></blockquote><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块化是一种分治的思想,通过分解复杂系统为独立的模块实现细粒度的精细控制,对于复杂系统的维护和管理十分有益。模块化也是组件化的基石,是构成现在色彩斑斓的前端世界的前提条件。</p><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">为什么需要模块化</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">前端开发和其他开发工作的主要区别,首先是前端是<span style="box-sizing: border-box;font-weight: bolder;">基于多语言</span>、<span style="box-sizing: border-box;font-weight: bolder;">多层次的编码和组织工作</span>,其次前端产品的交付是<span style="box-sizing: border-box;font-weight: bolder;">基于浏览器</span>,这些资源是通过增量加载的方式运行到浏览器端,如何在开发环境组织好这些碎片化的代码和资源,并且保证他们在浏览器端快速、优雅的加载和更新,就需要一个模块化系统。</p><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块化的好处</h2><ol style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;"><span style="box-sizing: border-box;font-weight: bolder;">可维护性。</span>&nbsp;因为模块是独立的,一个设计良好的模块会让外面的代码对自己的依赖越少越好,这样自己就可以独立去更新和改进。</p></li><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;"><span style="box-sizing: border-box;font-weight: bolder;">命名空间。</span>&nbsp;在 JavaScript 里面,如果一个变量在最顶级的函数之外声明,它就直接变成全局可用。因此,常常不小心出现命名冲突的情况。使用模块化开发来封装变量,可以避免污染全局环境。</p></li><li style="box-sizing: border-box;list-style: inherit;padding-left: 6px;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;"><span style="box-sizing: border-box;font-weight: bolder;">重用代码。</span>&nbsp;我们有时候会喜欢从之前写过的项目中拷贝代码到新的项目,这没有问题,但是更好的方法是,通过模块引用的方式,来避免重复的代码库。我们可以在更新了模块之后,让引用了该模块的所有项目都同步更新,还能指定版本号,避免 API 变更带来的麻烦。</p></li></ol><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块化简史</h2><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">1. 最简单粗暴的方式</h3><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">fn1</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br>}<br><br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">fn2</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br>}<br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">通过 script 标签引入文件,调用相关的函数。这样需要手动去管理依赖顺序,容易造成命名冲突,污染全局,随着项目的复杂度增加维护成本也越来越高。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">2. 用对象来模拟命名空间</h3><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;font-weight: 700;">var</span> output = {<br> <span style="box-sizing: border-box;">_count</span>: <span style="box-sizing: border-box;color: teal;">0</span>,<br> <span style="box-sizing: border-box;">fn1</span>: <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br> }<br>}<br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">这样可以解决上面的全局污染的问题,有那么点命名空间的意思,但是随着项目复杂度增加需要越来越多的这样的对象需要维护,不说别的,取名字都是个问题。最关键的还是内部的属性还是可以被直接访问和修改。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">3. 闭包</h3><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;font-weight: 700;">var</span> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span> = (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> _count = <span style="box-sizing: border-box;color: teal;">0</span>;<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> fn1 = <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br> }<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> fn2 = <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">fn2</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br> }<br> <span style="box-sizing: border-box;font-weight: 700;">return</span> {<br> <span style="box-sizing: border-box;">fn1</span>: fn1,<br> <span style="box-sizing: border-box;">fn2</span>: fn2<br> }<br>})()<br><br><span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>.fn1();<br><span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>._count; <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// undefined</span><br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">这样就拥有独立的词法作用域,内存中只会存在一份 copy。这不仅避免了外界访问此&nbsp;<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">IIFE</code>&nbsp;中的变量,而且又不会污染全局作用域,通过&nbsp;<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">return</code>&nbsp;暴露出公共接口供外界调用。这其实就是现代模块化实现的基础。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">4. 更多</h3><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">还有基于闭包实现的松耦合拓展、紧耦合拓展、继承、子模块、跨文件共享私有对象、基于 new 构造的各种方式,这种方式在现在看来都不再优雅。</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 松耦合拓展</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 这种方式使得可以在不同的文件中以相同结构共同实现一个功能块,且不用考虑在引入这些文件时候的顺序问题。</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 缺点是没办法重写你的一些属性或者函数,也不能在初始化的时候就是用module的属性。</span><br><span style="box-sizing: border-box;font-weight: 700;">var</span> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span> = (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">my</span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// ...</span><br> <span style="box-sizing: border-box;font-weight: 700;">return</span> my<br>})(<span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span> || {})<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 紧耦合拓展(没有传默认参数)</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 加载顺序不再自由,但是可以重载</span><br><span style="box-sizing: border-box;font-weight: 700;">var</span> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span> = (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">my</span>)</span>{<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> old = my.someOldFunc<br> <br> my.someOldFunc = <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 重载方法,依然可通过old调用旧的方法...</span><br> }<br><br> <span style="box-sizing: border-box;font-weight: 700;">return</span> my<br>})(<span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>)<br></code></pre><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">实现模块化的方案规范总览</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">历史上,JavaScript 一直没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。其他语言都有这项功能,比如 Ruby 的<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">require</code>、Python 的<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">import</code>,甚至就连 CSS 都有<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">@import</code>,但是 JavaScript 任何这方面的支持都没有,这对开发大型的、复杂的项目形成了巨大障碍。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6 模块的设计思想是尽量的静态化,使得编译时就能确定模块的依赖关系,以及输入和输出的变量。CommonJS 和 AMD 模块,都只能在运行时确定这些东西。比如,CommonJS 模块就是对象,输入时必须查找对象属性。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">目前实现模块化的规范主要有:</p><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>CommonJS</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>CMD</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>AMD</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>ES6模块</p></li></ul><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CommonJS()</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CommonJS 是以在浏览器环境之外构建 JavaScript 生态系统为目标而产生的项目,比如在服务器和桌面环境中。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">采用同步加载模块的方式,也就是说只有加载完成,才能执行后面的操作。CommonJS 代表:Node 应用中的模块,通俗的说就是你用 npm 安装的模块。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">它使用 require 引用和加载模块,exports 定义和导出模块,module 标识模块。使用 require 时需要去读取并执行该文件,然后返回 exports 导出的内容。</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//定义模块 math.js</span><br> <span style="box-sizing: border-box;font-weight: 700;">var</span> random=<span style="box-sizing: border-box;color: rgb(0, 134, 179);">Math</span>.random()*<span style="box-sizing: border-box;color: teal;">10</span>;<br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">printRandom</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(random)<br> }<br><br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">printIntRandom</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(<span style="box-sizing: border-box;color: rgb(0, 134, 179);">Math</span>.floor(random))<br> }<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//模块输出</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>.exports={<br> <span style="box-sizing: border-box;">printRandom</span>:printRandom,<br> <span style="box-sizing: border-box;">printIntRandom</span>:printIntRandom<br> }<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//加载模块 math.js</span><br> <span style="box-sizing: border-box;font-weight: 700;">var</span> math=<span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'math'</span>)<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//调用模块提供的方法</span><br> math.printIntRandom()<br> math.printRandom()<br></code></pre><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CommonJS主要用于服务端的模块化,不适用于前端模块化的原因在于:</h3><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>服务端加载一个模块,直接就从硬盘或者内存中读取了,消耗时间可以忽略不计</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>浏览器需要从服务端下载这个文件,所以说如果用CommonJS的require方式加载模块,需要等代码模块下载完毕,并运行之后才能得到所需要的API。</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>如果我们在某个代码模块里使用CommonJS的方法require了一个模块,而这个模块需要通过http请求从服务器去取,如果网速很慢,而CommonJS又是同步的,所以将阻塞后面代码的执行,从而阻塞浏览器渲染页面,使得页面出现假死状态。</p></li></ul><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CommonJS在浏览器端实现的步骤:</h3><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">1. 创建项目结构</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">|-js<br> |-dist <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//打包生成文件的目录</span><br> |-src <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//源码所在的目录</span><br> |-module1.js<br> |-module2.js<br> |-module3.js<br> |-app.js <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//应用主源文件</span><br>|-index.html <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//运行于浏览器上</span><br>|-package.json<br> {<br> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"name"</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"browserify-test"</span>,<br> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"version"</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"1.0.0"</span><br> }<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">2. 下载browserify</h4><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>全局: npm install browserify -g</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>局部: npm install browserify --save-dev</p></li></ul><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">3. 定义模块代码(同服务器端)</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">注意:<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">index.html</code>文件要运行在浏览器上,需要借助browserify将<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">app.js</code>文件打包编译,如果直接在<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">index.html</code>引入<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">app.js</code>就会报错!</p><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">4. 打包处理js</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">根目录下运行<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">browserify js/src/app.js -o js/dist/bundle.js</code></p><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">5. 页面使用引入</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">在index.html文件中引入<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">&lt;script type="text/javascript" src="js/dist/bundle.js"&gt;&lt;/script&gt;</code></p><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">AMD(Asynchronous Module Definition)</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">异步模块定义,所谓异步是指模块和模块的依赖可以被异步加载,他们的加载不会影响它后面语句的运行。有效避免了采用同步加载方式中导致的页面假死现象。AMD代表:RequireJS。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">AMD一开始是CommonJS规范中的一个草案,全称是Asynchronous Module Definition,即异步模块加载机制。后来由该草案的作者以RequireJS实现了AMD规范,所以一般说AMD也是指RequireJS。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">RequireJS是一个工具库,主要用于客户端的模块管理。它的模块管理遵守AMD规范,<span style="box-sizing: border-box;font-weight: bolder;">RequireJS的基本思想是,通过define方法,将代码定义为模块;通过require方法,实现代码的模块加载</span>。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">它主要有两个接口:<span style="box-sizing: border-box;font-weight: bolder;">define</span>&nbsp;和&nbsp;<span style="box-sizing: border-box;font-weight: bolder;">require</span>。define 是模块开发者关注的方法,而 require 则是模块使用者关注的方法。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">define() 函数:</h3><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">define(id?, dependencies?, factory);<br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//id :可选参数,它指的是模块的名字。</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//dependencies:可选参数,定义中模块所依赖模块的数组。</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//factory:模块初始化要执行的函数或对象</span><br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">需要注意的是,dependencies有多少个元素,factory就有多少个传参,位置一一对应。使用栗子:</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">define(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"alpha"</span>, [<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"require"</span>, <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"exports"</span>, <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"beta"</span>], <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, beta</span>) </span>{ <br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.verb = <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>) </span>{ <br> <span style="box-sizing: border-box;font-weight: 700;">return</span> beta.verb(); <br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//Or:</span><br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//return require("beta").verb(); </span><br> } <br>});<br></code></pre><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">require() 函数</h3><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>([<span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>], callback);<br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//module:一个数组,里面的成员就是要加载的模块.</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//callback:模块加载成功之后的回调函数。</span><br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">需要注意的是 ,module 有多少个元素,callback 就有多少个传参,位置一一对应。</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>([<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"a"</span>,<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"b"</span>,<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"c"</span>],<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">a,b,c</span>)</span>{ <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//code here });</span><br></code></pre><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">AMD的优缺点</h3><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">AMD 运行时核心思想是「Early Executing」,也就是提前执行依赖 AMD 的这个特性有好有坏:</p><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;">首先,尽早执行依赖可以尽早发现错误。  </p></li><li style="box-sizing: border-box;list-style: inherit;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;">另外,尽早执行依赖通常可以带来更好的用户体验,也容易产生浪费。</p></li><li style="box-sizing: border-box;list-style: inherit;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;">引用AMD的Javscript库:目前,主要有两个Javascript库实现了AMD规范:require.js和curl.js</p></li><li style="box-sizing: border-box;list-style: inherit;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;">在浏览器环境中异步加载模块;并行加载多个模块;</p></li><li style="box-sizing: border-box;list-style: inherit;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;">开发成本高,代码的阅读和书写比较困难,模块定义方式的语义不顺畅;不符合通用的模块化思维方式,是一种妥协的实现。</p></li></ul><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">AMD在浏览器端的实现步骤</h3><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">1. 下载require.js, 并引入</h4><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>官网:&nbsp;<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">http://www.requirejs.cn/</code></p></li><li style="box-sizing: border-box;list-style: inherit;"><p>github :&nbsp;<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">https://github.com/requirejs/requirejs</code></p></li></ul><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">然后将require.js导入项目: js/libs/require.js</p><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">2. 创建项目结构</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">|-js<br> |-libs<br> |-<span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>.js<br> |-modules<br> |-alerter.js<br> |-dataService.js<br> |-main.js<br>|-index.html<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">3. 定义require.js的模块</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// dataService.js文件</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 定义没有依赖的模块</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">let</span> msg = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'www.baidu.com'</span><br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">getMsg</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">return</span> msg.toUpperCase()<br> }<br> <span style="box-sizing: border-box;font-weight: 700;">return</span> { getMsg } <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 暴露模块</span><br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//alerter.js文件</span><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 定义有依赖的模块</span><br>define([<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'dataService'</span>], <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">dataService</span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">let</span> name = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'Tom'</span><br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">showMsg</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> alert(dataService.getMsg() + <span style="box-sizing: border-box;color: rgb(221, 17, 68);">', '</span> + name)<br> }<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 暴露模块</span><br> <span style="box-sizing: border-box;font-weight: 700;">return</span> { showMsg }<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// main.js文件</span><br>(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>.config({<br> <span style="box-sizing: border-box;">baseUrl</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'js/'</span>, <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//基本路径 出发点在根目录下</span><br> <span style="box-sizing: border-box;">paths</span>: {<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//映射: 模块标识名: 路径</span><br> <span style="box-sizing: border-box;">alerter</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./modules/alerter'</span>, <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//此处不能写成alerter.js,会报错</span><br> <span style="box-sizing: border-box;">dataService</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./modules/dataService'</span><br> }<br> });<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>([<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'alerter'</span>], <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">alerter</span>) </span>{<br> alerter.showMsg()<br> });<br>})();<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// index.html文件</span><br>&lt;!DOCTYPE html&gt;<br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">html</span>&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">head</span>&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">title</span>&gt;</span>Modular Demo<span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">title</span>&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">head</span>&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">body</span>&gt;</span><br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">&lt;!-- 引入require.js并指定js主文件的入口 --&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">script</span> <span style="box-sizing: border-box;color: teal;">data-main</span>=<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"js/main"</span> <span style="box-sizing: border-box;color: teal;">src</span>=<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"js/libs/require.js"</span>&gt;</span><span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">script</span>&gt;</span><br> <span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">body</span>&gt;</span><br><span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">html</span>&gt;</span></span><br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">4. 页面引入require.js模块:</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">在index.html引入&nbsp;<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">&lt;script data-main="js/main" src="js/libs/require.js"&gt;&lt;/script&gt;</code></p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;font-weight: bolder;">此外在项目中如何引入第三方库</span>?只需在上面代码的基础稍作修改:</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// alerter.js文件</span><br>define([<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'dataService'</span>, <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'jquery'</span>], <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">dataService, $</span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">let</span> name = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'Tom'</span><br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">showMsg</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> alert(dataService.getMsg() + <span style="box-sizing: border-box;color: rgb(221, 17, 68);">', '</span> + name)<br> }<br> $(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'body'</span>).css(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'background'</span>, <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'green'</span>)<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 暴露模块</span><br> <span style="box-sizing: border-box;font-weight: 700;">return</span> { showMsg }<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// main.js文件</span><br>(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>.config({<br> <span style="box-sizing: border-box;">baseUrl</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'js/'</span>, <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//基本路径 出发点在根目录下</span><br> <span style="box-sizing: border-box;">paths</span>: {<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//自定义模块</span><br> <span style="box-sizing: border-box;">alerter</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./modules/alerter'</span>, <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//此处不能写成alerter.js,会报错</span><br> <span style="box-sizing: border-box;">dataService</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./modules/dataService'</span>,<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 第三方库模块</span><br> <span style="box-sizing: border-box;">jquery</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./libs/jquery-1.10.1'</span> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//注意:写成jQuery会报错</span><br> }<br> })<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>([<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'alerter'</span>], <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;">alerter</span>) </span>{<br> alerter.showMsg()<br> })<br>})()<br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">上例是在alerter.js文件中引入jQuery第三方库,main.js文件也要有相应的路径配置。&nbsp;<span style="box-sizing: border-box;font-weight: bolder;">小结</span>:通过两者的比较,可以得出<span style="box-sizing: border-box;font-weight: bolder;">AMD模块定义的方法非常清晰,不会污染全局环境,能够清楚地显示依赖关系</span>。AMD模式可以用于浏览器环境,并且允许非同步加载模块,也可以根据需要动态加载模块。</p><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">UMD规范</h2><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CMD(Common Module Definition)</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CMD是SeaJS在推广过程中生产的对模块定义的规范,在Web浏览器端的模块加载器中,SeaJS与RequireJS并称,SeaJS作者为阿里的玉伯。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CMD规范专门用于浏览器端,模块的加载是异步的,模块使用时才会加载执行。CMD规范整合了CommonJS和AMD规范的特点。在 Sea.js 中,所有 JavaScript 模块都遵循 CMD模块定义规范。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CMD语法</h3><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;font-weight: bolder;">定义暴露模块:</span></h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//定义没有依赖的模块</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.xxx = value<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>.exports = value<br>})<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//定义有依赖的模块</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>)</span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//引入依赖模块(同步)</span><br> <span style="box-sizing: border-box;font-weight: 700;">var</span> module2 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module2'</span>)<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//引入依赖模块(异步)</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>.async(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module3'</span>, <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;">m3</span>) </span>{<br> })<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//暴露模块</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.xxx = value<br>})<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;font-weight: bolder;">引入使用模块:</span></h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span></span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> m1 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module1'</span>)<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> m4 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module4'</span>)<br> m1.show()<br> m4.show()<br>})<br></code></pre><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">CMD的优缺点</h3><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>优点:依赖就近,延迟执行 可以很容易在 Node.js 中运行;</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>缺点:依赖 SPM 打包,模块的加载逻辑偏重;</p></li></ul><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">sea.js使用步骤</h3><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">1. 下载sea.js, 并引入</h4><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>官网:&nbsp;seajs.org/</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>github :&nbsp;github.com/seajs/seajs</p></li></ul><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">然后将sea.js导入项目: js/libs/sea.js</p><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">2. 创建项目结构</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">|-js<br> |-libs<br> |-sea.js<br> |-modules<br> |-module1.js<br> |-module2.js<br> |-module3.js<br> |-module4.js<br> |-main.js<br>|-index.html<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">3. 定义sea.js的模块代码</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// module1.js文件</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//内部变量数据</span><br> <span style="box-sizing: border-box;font-weight: 700;">var</span> data = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'atguigu.com'</span><br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//内部函数</span><br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">show</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'module1 show() '</span> + data)<br> }<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//向外暴露</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.show = show<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// module2.js文件</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span>.exports = {<br> <span style="box-sizing: border-box;">msg</span>: <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'I Will Back'</span><br> }<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// module3.js文件</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span>(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">const</span> API_KEY = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'abc123'</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.API_KEY = API_KEY<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// module4.js文件</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>, <span style="box-sizing: border-box;color: rgb(0, 134, 179);">module</span></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//引入依赖模块(同步)</span><br> <span style="box-sizing: border-box;font-weight: 700;">var</span> module2 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module2'</span>)<br> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">show</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'module4 show() '</span> + module2.msg)<br> }<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">exports</span>.show = show<br> <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">//引入依赖模块(异步)</span><br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>.async(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module3'</span>, <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;">m3</span>) </span>{<br> <span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'异步引入依赖模块3 '</span> + m3.API_KEY)<br> })<br>});<br><br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// main.js文件</span><br>define(<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> (<span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span></span>) </span>{<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> m1 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module1'</span>)<br> <span style="box-sizing: border-box;font-weight: 700;">var</span> m4 = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">require</span>(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./module4'</span>)<br> m1.show()<br> m4.show()<br>})<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">4. 在index.html中引入</h4><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;">&lt;script type=<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"text/javascript"</span> src=<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"js/libs/sea.js"</span>&gt;&lt;/script&gt;<br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;color: navy;">&lt;<span style="box-sizing: border-box;">script</span> <span style="box-sizing: border-box;color: teal;">type</span>=<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"text/javascript"</span>&gt;</span><span style="box-sizing: border-box;"><br> seajs.use(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./js/modules/main'</span>)<br></span><span style="box-sizing: border-box;color: navy;">&lt;/<span style="box-sizing: border-box;">script</span>&gt;</span></span><br></code></pre><h2 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 12px;font-size: 20px;border-bottom: 1px solid rgb(236, 236, 236);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6模块化(==重点介绍==)</h2><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6模块的设计思想,是尽量的静态化,使得编译时就能确定模块的依赖关系,以及输入和输出的变量。所以说ES6是<span style="box-sizing: border-box;font-weight: bolder;">编译时加载</span>,不同于CommonJS的运行时加载(实际加载的是一整个对象),ES6模块不是对象,而是通过export命令显式指定输出的代码,输入时也采用静态命令的形式。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6 的模块自动采用严格模式,不管你有没有在模块头部加上<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">"use strict";</code>。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">严格模式主要有以下限制。</p><ul style="padding-left: 28px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);" class="list-paddingleft-2"><li style="box-sizing: border-box;list-style: inherit;"><p>变量必须声明后再使用</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>函数的参数不能有同名属性,否则报错</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">with</code>语句</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能对只读属性赋值,否则报错</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能使用前缀 0 表示八进制数,否则报错</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能删除不可删除的属性,否则报错</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能删除变量<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">delete prop</code>,会报错,只能删除属性<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">delete global[prop]</code></p></li><li style="box-sizing: border-box;list-style: inherit;"><p><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">eval</code>不会在它的外层作用域引入变量</p></li><li style="box-sizing: border-box;list-style: inherit;"><p><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">eval</code>和<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">arguments</code>不能被重新赋值</p></li><li style="box-sizing: border-box;list-style: inherit;"><p><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">arguments</code>不会自动反映函数参数的变化</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">arguments.callee</code></p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">arguments.caller</code></p></li><li style="box-sizing: border-box;list-style: inherit;"><p>禁止<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">this</code>指向全局对象</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>不能使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">fn.caller</code>和<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">fn.arguments</code>获取函数调用的堆栈</p></li><li style="box-sizing: border-box;list-style: inherit;"><p>增加了保留字(比如<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">protected</code>、<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">static</code>和<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">interface</code>)</p></li></ul><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">上面这些限制,模块都必须遵守。由于严格模式是 ES5 引入的,不属于 ES6,所以请参阅相关 ES5 书籍,本书不再详细介绍了。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">其中,尤其需要注意<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">this</code>的限制。ES6 模块之中,顶层的<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">this</code>指向<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">undefined</code>,即不应该在顶层代码使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">this</code>。</p><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">语法</h3><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">模块功能主要由两个命令构成:export和import。export命令用于规定模块的对外接口,import命令用于输入其他模块提供的功能。</p><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">export</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">一个模块就是一个独立的文件。该文件内部的所有变量,外部无法获取。如果你希望外部能够读取模块内部的某个变量,就必须使用export关键字输出该变量。下面是一个 JS 文件,里面使用export命令输出变量。</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">cUl</span>(<span style="box-sizing: border-box;"></span>)</span>{<br> <span style="box-sizing: border-box;font-weight: 700;">let</span> ulEle = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">document</span>.createElement(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"ul"</span>);<br> <span style="box-sizing: border-box;font-weight: 700;">for</span>(<span style="box-sizing: border-box;font-weight: 700;">let</span> i = <span style="box-sizing: border-box;color: teal;">0</span>; i &lt; <span style="box-sizing: border-box;color: teal;">5</span>; i++){<br> <span style="box-sizing: border-box;font-weight: 700;">let</span> liEle = <span style="box-sizing: border-box;color: rgb(0, 134, 179);">document</span>.createElement(<span style="box-sizing: border-box;color: rgb(221, 17, 68);">"li"</span>);<br> liEle.innerHTML = <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"无序列表"</span> + i;<br> ulEle.appendChild(liEle);<br> }<br> <span style="box-sizing: border-box;font-weight: 700;">return</span> ulEle;<br>}<br><span style="box-sizing: border-box;font-weight: 700;">let</span> ul = cUl();<br><span style="box-sizing: border-box;font-weight: 700;">export</span> {ul};<br></code></pre><h4 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;padding-bottom: 5px;font-size: 20px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">import</h4><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">使用<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">export</code>命令定义了模块的对外接口以后,其他 JS 文件就可以通过<code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 0.87em;word-break: break-word;border-radius: 2px;overflow-x: auto;background-color: rgb(255, 245, 245);color: rgb(255, 80, 44);padding: 0.065em 0.4em;">import</code>命令加载这个模块。</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;font-weight: 700;">import</span> {table} <span style="box-sizing: border-box;font-weight: 700;">from</span> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"../test/test_table.js"</span>;<br><span style="box-sizing: border-box;font-weight: 700;">import</span> {div} <span style="box-sizing: border-box;font-weight: 700;">from</span> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"../test/test_div.js"</span> ;<br><span style="box-sizing: border-box;font-weight: 700;">import</span> {ul} <span style="box-sizing: border-box;font-weight: 700;">from</span> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">"../test/test_ul.js"</span> ;<br><br><span style="box-sizing: border-box;font-weight: 700;">export</span> {table, div, ul};<br></code></pre><h3 style="box-sizing: border-box;line-height: 1.5;margin-top: 35px;margin-bottom: 10px;font-size: 18px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6 模块与 CommonJS 模块的差异</h3><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">它们有两个重大差异:</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;font-weight: bolder;">1. CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用</span>。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box;font-weight: bolder;">2. CommonJS 模块是运行时加载,ES6 模块是编译时输出接口</span>。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">第二个差异是因为 CommonJS 加载的是一个对象(即module.exports属性),该对象只有在脚本运行完才会生成。而 ES6 模块不是对象,它的对外接口只是一种静态定义,在代码静态解析阶段就会生成。</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">下面重点解释第一个差异,我们还是举上面那个CommonJS模块的加载机制例子:</p><pre style=" box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 16px;overflow: auto; line-height: 1.75;background: rgb(248, 248, 248);text-align: start; "><code style="box-sizing: border-box;font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;font-size: 12px;word-break: normal;border-radius: 2px;overflow-x: auto;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;padding: 15px 12px;display: block;"><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// lib.js</span><br><span style="box-sizing: border-box;font-weight: 700;">export</span> <span style="box-sizing: border-box;font-weight: 700;">let</span> counter = <span style="box-sizing: border-box;color: teal;">3</span>;<br><span style="box-sizing: border-box;font-weight: 700;">export</span> <span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;">function</span> <span style="box-sizing: border-box;color: rgb(153, 0, 0);font-weight: 700;">incCounter</span>(<span style="box-sizing: border-box;"></span>) </span>{<br> counter++;<br>}<br><span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// main.js</span><br><span style="box-sizing: border-box;font-weight: 700;">import</span> { counter, incCounter } <span style="box-sizing: border-box;font-weight: 700;">from</span> <span style="box-sizing: border-box;color: rgb(221, 17, 68);">'./lib'</span>;<br><span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(counter); <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 3</span><br>incCounter();<br><span style="box-sizing: border-box;color: rgb(0, 134, 179);">console</span>.log(counter); <span style="box-sizing: border-box;color: rgb(153, 153, 136);font-style: italic;">// 4</span><br></code></pre><p style="box-sizing: border-box;line-height: inherit;margin-top: 22px;margin-bottom: 22px;font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;background-color: rgb(255, 255, 255);">ES6 模块的运行机制与 CommonJS 不一样。<span style="box-sizing: border-box;font-weight: bolder;">ES6 模块是动态引用,并且不会缓存值,模块里面的变量绑定其所在的模块</span>。</p><blockquote style="box-sizing: border-box;color: rgb(102, 102, 102);padding: 1px 23px;margin-top: 22px;margin-bottom: 22px;border-left-width: 4px;border-left-color: rgb(203, 203, 203);background-color: rgb(248, 248, 248);font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;;font-size: 16px;text-align: start;white-space: normal;"><p style="box-sizing: border-box;line-height: inherit;margin-top: 10px;margin-bottom: 10px;">学习有趣的知识,结识有趣的朋友,塑造有趣的灵魂!</p><p style="box-sizing: border-box;line-height: inherit;margin-top: 10px;margin-bottom: 10px;">大家好,我是〖编程三昧〗的作者&nbsp;<span style="box-sizing: border-box;font-weight: bolder;">隐逸王</span>,我的公众号是『编程三昧』,欢迎关注,希望大家多多指教!</p></blockquote></div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值