CommonJS

CommonJs

Loading modules uses the require method, which is the specification adopted by Node.js

// foobar.js
 
// Private variables
var test = 123;
 
// Public method
function foobar () {
 
    this.foo = function () {
        // do someing ...
    }
    this.bar = function () {
        //do someing ...}}Methods and variables on exports objects are public
var foobar = new foobar();
exports.foobar = foobar;
Copy the code
The //require method reads js files by default, so you can omit the js suffix
var test = require('./boobar').foobar;
 
test.bar();
Copy the code

AMD

AMD is the canonical output of RequireJS’s module definition in its promotion process

Require.js browser side, AMD is ahead of execution

define(id? , dependencies? , factory);

  • The first parameter ID is a string that represents the module id and is optional. If not, the module id should be defined by default as the id of the requested script in the loader. If present, the module id must be top-level or an absolute id.

  • The second parameter, dependencies, is an array literal that identifies the modules that the current module depends on.

  • The third argument, factory, is a function or object that needs to be instantiated.

define("alpha"["require"."exports"."beta"].function( require.exports, beta ){
    export.verb = function(){
        return beta.verb();
        // or:
        return require("beta").verb(); }});Copy the code

CMD

CMD is the normalized output of SeaJS module definitions during the roll-out process

AMD advocates the dependency front, CMD advocates the dependency nearby.

define(function (requie, exports.module) {
     
    // Dependencies can be written nearby
    var a = require('./a'); a.test(); ./ / soft dependency
    if (status) {
     
        var b = requie('./b'); b.test(); }});Copy the code

The differences between AMD and CDM are as follows: (1) For dependent modules, AMD preempts execution (and seems to be able to delay execution now), CMD delays execution. (2) AMD advocates relying on the front, CMD advocates relying on nearby. (3) AMD advocates reuse interface, CMD advocates single-use interface. (4) Differences in writing norms.

UMD

Umd is a mashup of AMD and CommonJS

UMD determines whether any module (exports) that supports Node.js exists and uses node.js module mode if it does.

Check whether AMD is supported (define exists). If AMD exists, load modules in AMD mode.