美文网首页
Js module style introduction

Js module style introduction

作者: a_pioneer | 来源:发表于2017-06-21 08:51 被阅读0次

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.

相关文章

网友评论

      本文标题:Js module style introduction

      本文链接:https://www.haomeiwen.com/subject/psdzqxtx.html