Amd,cmd.commonjs and UMD, and ES6 's modular comparison.

Source: Internet
Author: User

CommonJS

COMMONJS is a specification for server-side modules, and node. JS uses this specification.

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.

For example:

//Foobar.js //private variable var test = 123; //Public method function foobar () { This.foo = function () { //do someing ...} This.bar = function () { //do someing ...}} the methods and variables on the//exports object are public var foobar = new Foobar (); exports.foobar = Foobar;     

AMD CMD

The AMD specification implementation represents the Require.js

AMD is an asynchronous load specification

The implementation of the CMD specification represents the Sea.js

    • For dependent modules AMD is executed ahead of time, and CMD is deferred execution. However, starting from 2.0, the Requirejs is also changed to be deferred (depending on how it is written, the processing does not pass).

    • The cmd is highly dependent on the nearest, AMD is highly dependent on the predecessor.

 //amd  define ([ Span class= "hljs-string", "/A",  "./b",  function (a, b) {//dependency from the beginning, write well A.test (); B.test ();}); //cmd  define (function (requie, exports, module) {// Reliance can be written near var a = require (//soft dependency if (status) {var B = Requie ( Span class= "hljs-string" and "./b"); B.test (); }}); 

/span>

Although AMD also supports CMD notation, relying on the predecessor is the default module definition for official documents.

    • The AMD API defaults to one when multiple uses, the CMD strict distinction is respected as a single responsibility. For example: AMD require is global and local. CMD does not have a global require, providing seajs.use () to enable the loading of the module system to start. Each API in CMD is simple and pure.

Simply put, sea.js belongs to lazy loading, require.js belongs to preload.

Here, by the way, extend the pros and cons of preloading and lazy loading

Preload: Loads all files when first accessed

Advantage: Once the first visit is completed, the speed of the visit will be fast

Cons: The first time you load a page waits a long time.

Lazy loading: The corresponding files are loaded when they are used.

Advantage: The first access speed is relatively fast.

Disadvantage: The speed of accessing other new modules becomes slower.

Umd

UMD is a blend of AMD and COMMONJS

AMD modules evolve with the first principle of the browser, loading modules asynchronously.
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 (typeof exports = =  ' object ') {module.exports = Factory ();} else if (typeof define = = = else {window.eventutil = Factory ();}) (this, function (//module ...});         
 

Amd,cmd.commonjs and UMD, and ES6 's modular comparison.

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.