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