CommonJS, AMD, and CMD

Source: Internet
Author: User

COMMONJS is used in server-side, synchronous, such as Nodejs; Amd,cmd is used on the browser side, AMD is asynchronous module loaded, such as Requirejs;cmd is a generic module loaded, such as SEAJS. Among them, AMD first proposed, CMD is based on COMMONJS and AMD based on the proposed. COMMONJSCOMMONJS is a specification for server-side modules, and node. JS uses this specification. According to the COMMONJS specification, a single file is a module. The load module uses the Require method, which reads a file and executes it, and finally returns the exports object inside the file. Example: Foobar.js
/private variable var test = 123;  Public method function Foobar () {      This.foo = function () {         //do someing ...     }     This.bar = function () {         //do someing ...     }}  

Using Foobar.js

The Require method reads the JS file by default, so you can omit the JS suffix var test = require ('./boobar '). Foobar; Test.bar ();

The CommonJS load module is synchronous, so only the loading is complete to perform subsequent operations. Like node. JS is primarily used for server programming, the loaded module files are generally already present on the local hard disk, so it is faster to load, regardless of the way asynchronous loading, so the COMMONJS specification is more applicable. However, in the case of a browser environment, to load modules from the server, this is the asynchronous pattern that must be used. So there's the AMD CMD solution.

AMD (asynchromous Module Definition)

AMD is the normalized output of the module defined by REQUIREJS during the promotion process. AMD loads the module asynchronously. Its modules support various types of modules such as Object function constructor string json.

The applicable AMD specification applies the Define method definition module. As follows:

By introducing a dependency through an array, the callback function passes through the parameter to the dependent define ([' SomeModule1 ', ' someModule2 '], function (SomeModule1, someModule2) {      function foo () {///         someing         somemodule1.test ();     }      

The AMD specification allows the output module to be compatible with the COMMONJS specification, when the Define method is as follows:

Define (function (Require, exports, module) {          var reqmodule = require ("./somemodule");     Requmodule.test ();          Exports.asplode = function () {         //someing     
Cmd

CMD is the normalized output of the module defined by SEAJS in the process of generalization. The difference between CMD and AMD has the following points:

1. For dependent modules AMD is executed ahead of time, and CMD is deferred execution. However, starting from 2.0, the Requirejs is also changed to be deferred (depending on how it is written, the processing does not pass).

2.CMD is highly dependent on proximity, AMD is highly reliant on pre-built.

AMD define (['./a ', './b '), function (A, b) {      //depend on the beginning to write     a.test ();     
CMD Define (function (Requie, exports, module) {          //dependency can be written to the nearest     var a = require ('./a ');     A.test ();          ...     Soft-dependent     if (status) {              var b = Requie ('./b ');         B.test ();     

Although AMD also supports CMD notation, relying on the predecessor is the default module definition for official documents.

The 3.AMD API defaults to one when multiple uses, the CMD strict distinction is respected as a single responsibility. For example: AMD require is global and local. CMD does not have a global require, providing seajs.use () to enable the loading of the module system to start. Each API in CMD is simple and pure.

Refer to the experience of other bloggers and some personal summary, if there is infringement, please contact me in time, qq:435641688, thank you

CommonJS, AMD, and CMD

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.