The difference between Common JS, AMD, cmd, and UMD

Source: Internet
Author: User

First, CommonJS
The 1.CommonJS API defines the APIs that are used by many common applications (primarily non-browser apps). Its ultimate goal is to provide a library of standards like Python,ruby and Java. CommonJs is a specification for server-side modules, and node. JS uses this specification.

2. These specifications cover modules, binary, Buffer, character set encoding, I/O flow, process environment, file system, sockets, unit tests, breaches, server gateway interfaces, package management, etc.

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

The 4.CommonJS load modules are synchronous, so only the loading is complete to perform the 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.

Second, AMD, CMD
1.AMD is the normalized output of the module defined by Requirejs in the promotion process. CMD is the normalized output of the module defined by SEAJS in the process of generalization.

These specifications are intended for the modular development of JavaScript, especially on the browser side. At present, the implementation of these specifications can achieve the purpose of browser-side modular development.
2. Difference:
-For dependent modules, AMD is executed ahead of time, and CMD is deferred execution. However, starting from 2.0, Requirejs is also changed to be deferred (depending on the way it is written and handled differently). CMD respected as lazy as possible.
-AMD is highly dependent on the predecessor, and CMD is highly dependent on the nearest.

The default recommended by AMD is

define (['./a ', './b '), function (A, B) {//dependencies must be written at the beginning.
A.dosomething ()//Omit 100 lines here
B.dosomething ()
...
})



Cmd

Define (function(require, exports, module) {           var a = require ('./a ')           A.dosomething ()   //  omit 100   rows        var/ / dependency can be written near   B.dosomething ()   ...         })

While AMD supports the syntax of CMD, it also supports passing require as a dependency, but Requirejs's author defaults to the preferred notation, which is the default definition of the module in the official document.
-AMD's API defaults to one when multiple uses, the CMD API is strictly differentiated, advocating a single responsibility. For example, AMD, require global require and local require, are called require. In CMD, there is no global require, but according to the completeness of the module system, provide seajs.use to realize the loading and starting of the module system. CMD, each API is simple and pure.

3.AMD Asynchronous Load module. Its modules support various types of modules such as Object function constructor string json.

The applicable AMD specification applies the Define method definition module.

Third, UMD
-UMD is a blend of AMD and COMMONJS

-AMD Browser First principle of development asynchronous loading module.

-CommonJS module with the first principle of server development, choose Synchronous loading, its modules without packaging (unwrapped modules).

This forces people to come up with another more generic pattern UMD (Universal Module Definition). Want to solve the cross-platform solution.

UMD first determines whether the module (exports) that supports node. JS is present, and the node. JS module pattern exists.

In determining whether AMD is supported (define exists), the module is loaded using AMD mode.

(function(window, factory) {if(typeofExports = = = ' object ') {Module.exports=Factory (); } Else if(typeofdefine = = = ' function ' &&define.amd) {define (factory); } Else{window.eventutil=Factory (); }    })( This,function () {        //module ...});

The difference between Common JS, AMD, cmd, and UMD

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.