Commonjs the difference between AMD cmd

Source: Internet
Author: User

A blog to tell you the difference between the three: http://zccst.iteye.com/blog/2215317

Tell you the difference between the three and Requirejs Seajs: http://blog.chinaunix.net/uid-26672038-id-4112229.html

This humane hearing of the passers-by:

COMMONJS Specifications in node has not been produced before, there is, Commonjs as a back-end of the specification, although the back end of JS before the development of slow, but COMMONJS this specification is relatively perfect, with the popularization of node, COMMONJS is fully complied with and realized, COMMONJS is widely spread.

Commonjs are used in server-side, synchronous, such as Nodejs
AMD, CMD is used in browser-side, asynchronous, such as Requirejs and SEAJS

The AMD specification proposes modularity for the browser side, and CMD is based on the specifications of AMD and COMMONJS.

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.

For example:
Foobar.js

Private variables
var test = 123;

Public methods
function Foobar () {

This.foo = function () {
Do someing ...
}
This.bar = function () {
Do someing ...
}
}

The methods and variables on the exports object are public
var foobar = new Foobar ();
Exports.foobar = Foobar;

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 in 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.

A dependency is introduced through an array, and the callback function passes through the parameter to the dependency
define ([' SomeModule1 ', ' someModule2 '], function (SomeModule1, someModule2) {

function foo () {
Someing
Somemodule1.test ();
}

return {Foo:foo}
});
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   defined by SEAJS in the promotion process;

CMD differs from AMD in 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 the nearest, AMD is highly dependent on the predecessor.  
//amd 
define (['./a ', './b '], function (A, b) { 

   //dependencies start with  
    a.test ();  
    b.test ();  
});  

//cmd 
Define (function (Requie, exports, module) { 
     
   // Dependency can be written  
    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.

The main differences between SEAJS and Requirejs are explained here.

Commonjs the difference between AMD 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.