Js module style introduction

how to define dependencies: <script>-tag, CommonJS, AMD,ES6, …

<script>-tag

Common problems:

  1. Conflicts in the global object.
  2. Order of loading is important.
  3. Developers have to resolve dependencies of modules/libraries.
  4. In big projects the list can get really long and difficult to manage.

CommonJS: synchronous require

base: synchronous require && exported interface module.exports. It used server-side by node.js.

Common problems:

  1. Blocking calls do not apply well on networks. Network requests are asynchronous.
  2. No parallel require of multiple modules.

AMD: asynchronous require

base: require && define . It used by require.js

Pros:

  1. Fits the asynchronous request style in network.
  2. Parallel loading of multiple modules.

Cons:

  1. Coding overhead. More difficult to read and write.
  2. Seems to be some kind of workaround.

ES6 Modules

base: import && export && module

Pros:

  1. Static analysis is easy.
  2. Future-proof as ES standard.

Cons

  1. Native browser support will take time.
  2. Very few modules in this style.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 和闺蜜约烧烤 渠艺 2017.06.10
    渠六亿阅读 1,508评论 7 0
  • 记得那时候 你悄悄的来 忽的 整个世界便都是你 我呼吸 是你的气息 淡淡的 说不出的味道 我凝望 是你的身影 朦朦...
    老男人树先生阅读 994评论 3 7
  • 巴西向来不是一个认真的国家,在开幕式之前种种对里约的实力吐槽充斥各国媒体。当年申奥成功之后出现的里约狂欢已经远去,...
    说你是只兔子阅读 2,325评论 0 1