The difference between Commonjs,amd,requirejs

Source: Internet
Author: User


The REQUIREJS implements the AMD API.

Commonjs is used exports object to define a module, which defines the contents of the module. Simply implement a COMMONJS definition as follows:

Somemodule.js

exports.dosomething = function () {return ' foo ';};

Othermodule.js

var somemodule = require (' Somemodule '); In the vein of node

Exports.dosomethingelse = function () {return somemodule.dosomething () + "Bar";};

basically commonjs clear you need to have a require function to get the dependency, exports variables to output the contents of the module and some of the module identifiers used to get dependencies. COMMONJS has many implementations, such as node. js.

Because COMMONJS is not designed to look at the browser, it is not suitable for the browser environment (I am actually not clear about this, but this is everywhere, such as Requirejs official website). So we have to do some work to implement asynchronous loading.

instead, Requirejs implements AMD, which is designed to adapt to the browser environment. On the face of it, AMD started out as a byproduct of the COMMONJS output format and eventually evolved its own API. The new thing that appears in AMD is the define function, which allows the module to declare its dependencies before loading dependencies. For example, the definition might look like this:

Define (' module/id/string ', [' module ', ' dependency ', ' array '),

function (module, factory function) {

return modulecontents;

});

So commonjs and AMD are different implementations of the JavaScript module definition API, but they have the same root cause. AMD is more suitable for browsers because it supports asynchronous loading of module dependencies. Requirejs is an implementation of AMD and retains the spirit of COMMONJS (primarily the module identifier). Even more confusing is that Requirejs, while implementing AMD, also provides a COMMONJS package so that COMMONJS modules can be introduced almost directly by Requirejs.

Define (function (Require, exports, module) {

var somemodule = require (' Somemodule '); In the vein of node

Exports.dosomethingelse = function () {return somemodule.dosomething () + "Bar";};

});

The difference between Commonjs,amd,requirejs

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.