Commonjs,amd,cmd differences

Source: Internet
Author: User

Learn to be more dizzy, see Commonjs,amd again, cmd as if still not completely clear, today and then tidy up:
Commonjs are used in server-side, synchronous, such as Nodejs
AMD, CMD is used in browser-side, asynchronous, such as Requirejs and SEAJS
Among them, AMD first proposed, CMD is based on COMMONJS and AMD based on the proposed.

Why are you dizzy? It is because it is useless or used too little. It's no good to look at the article.



CommonJS

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

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 of the module definition in the SEAJS process.

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) {

Rely on the beginning to write well
A.test ();
B.test ();
});

Cmd
Define (function (Requie, exports, module) {

Reliance can be written near
var a = require ('./a ');
A.test ();

...
Soft dependency
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.

Original Address http://zccst.iteye.com/blog/2215317

Commonjs,amd,cmd differences

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.