Analysis of JS module specification: AMD,CMD,COMMONJS

Source: Internet
Author: User
Tags lua

From:https://www.jianshu.com/p/09ffac7a3b2c

With the development of JS Modular programming, the dependence between the processing modules becomes the key to maintain.


Modular

Amd,cmd,commonjs is currently the most commonly used three modular writing specifications.

CommonJS

The COMMONJS specification was born earlier. Nodejs adopted the COMMONJS. This is how the module is loaded:

clock = require(‘clock‘);clock.start();

This is a good fit for the server, because the servers read the modules are on the local disk and load fast. However, if the module is loaded at the client, it is possible to have a "suspended animation" condition. In the example above, the clock's call must wait for the Clock.js request to succeed and the load is complete. So, can the module be loaded asynchronously?

Amd

AMD, (asynchronous Module Definition), this specification is an asynchronous loading module, Requirejs applies this specification. Define all dependencies first, and then execute them in the callback function after the load is complete:

require([module], callback);

Write a module with AMD:

require([‘clock‘],function(clock){ clock.start();});

Although AMD implemented asynchronous loading, but began to write all the dependencies are not in accordance with the logical order of writing, can be as commonjs as the use of the time to require, but also support asynchronous loading and then execute it?

Cmd

CMD (Common Module Definition), is seajs respected norms, CMD is dependent on the nearest, when used again require. It's written like this:

define(function(require, exports, module) {   var clock = require(‘clock‘);   clock.start();});

The biggest difference between AMD and CMD is that the timing of the dependent modules is different, rather than the timing or manner of loading, both of which are asynchronous loading modules.
AMD relies on the front, JS can easily know the dependent module is who, immediately loaded, and CMD close to rely on, need to use the module to parse the string to know that relies on those modules, which is also a lot of people criticized the CMD, sacrificing performance to bring the convenience of development, in fact, the resolution module with a short time to be ignored.

The above is the similarities and differences between the three, if there are questions or suggestions, please refer to the following articles or contact me, thank you.
</br></br>



Riding the Wind
Links: https://www.jianshu.com/p/09ffac7a3b2c
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Analysis of JS module specification: AMD,CMD,COMMONJS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.