The difference between CommonJS, AMD, and CMD

Source: Internet
Author: User

JS Module Specification (commonjs,amd,cmd), if you have heard the JS modular this thing, then you should have heard or CommonJS or AMD or even CMD these specifications, I have heard, but also really listen to it. Now look at what these specs are, and why.

First, CommonJS

COMMONJS is for the performance of JS to make norms, Nodejs is the implementation of this specification, Webpack is also in the form of Commonjs to write. Because JS does not have the function of module, so Commonjs came into being, it hope JS can run anywhere, not just in the browser. Its ultimate goal is to provide a library of standards like Python,ruby and Java.

Commonjs defined module is divided into: {Module reference (require)} {module definition (exports)} {module identification (modules)}

Require () is used to introduce external modules; The exports object is used to export the method or variable of the current module, the only export, and the module object represents the modules themselves.

Although node follows Commonjs's specifications, it has made some trade-offs and filled in some new things.

However, said Commonjs also said node, then I think I have to understand the NPM. NPM, as node's package Manager, is not to help node with the installation of the dependency package, it must also follow the COMMONJS specification, which follows the package specification (or theory).

CommonJS Wiki speaks about its history, and also introduces modules and packages.

Second, AMD

Commonjs is mainly for JS in the back end of the performance of the formulation, he is not suitable for the front-end, why so to say?

This needs to analyze the browser-side JS and server-side JS are the main things to do, what is different:

Thus, AMD (asynchronous module definition) appears, it is mainly for the front-end JS performance specifications.

AMD has only one interface: define (id?,dependencies?,factory);

It's going to make all the dependencies (DEP) when declaring the module, and it's going to be passed as a parameter to factory, like this:

1 define ([' Dep1 ', ' Dep2 '],function (DEP1,DEP2) {...});

If there is no dependency, define a simple module, the following is OK:

1 define (function () {2     var exports = {};3     Exports.method = function () {...}; 4     return exports;5});

Hey, here are define, wrap things up, that node implementation how did not see there is define keyword, it also want to wrap things up ah, in fact, it is only node implicit packaging just .....

Requirejs is the implementation of the AMD specification.

This has the Chinese version of the wiki of AMD, said a lot of pretty detailed things, when used to see: AMD Wiki Chinese version

Third, CMD

Famous Yuber wrote Seajs, is to follow his proposed CMD specification, and AMD pretty similar, but it feels more convenient, the most important is the Chinese version, everything: Seajs official doc

1 define (function (Require,exports,module) {...});

Have used Seajs, this is not strange, right.

Earlier said AMD, said Requirejs realized Amd,cmd looks like AMD, that Requirejs and seajs like it?

Although the CMD and AMD pretty like, but the difference is quite obvious, the official unofficial have elaborated and understand, I think, said is very good:

Official exposition of similarities and differences between SEAJS and Requirejs

Seajs and Requirejs the most similarities and differences (This said also very good)

The difference between 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.