AMD, CMD, COMMONJS specification

Source: Internet
Author: User

AMD, CMD, COMMONJS specification

The idea of splitting the JS code into small chunks with different functions is popular in some tripartite specifications, such as COMMONJS, AMD, and CMD. Let's take a look at these kinds of specifications.

I. Modular specification

Commonjs is used on the server side, is synchronous loading, Nodejs is the practice of this specification.

Amd,cmd is used on the browser side, is asynchronous loaded, require.js and sea.js are dependent on this specification implementation.

1.1 COMMONJS Specification Example

COMMONJS module definition is very simple, mainly divided into module definition, module reference, module identification.

According to this specification, a JS file is a module that has its own scope and does not pollute the global scope. variables, functions, classes defined in a file are private and not visible to other files.

In the module, there is a Module object, which represents the module itself. Where exports is the module's property, which is an object that is used to mount the current module's method or variable, and is also the only export of the current module. For use by other modules.

1 Addtwo.js 2 -----------------------------------------3  function(A, b) {4        return a +b; 5  }   // Export Module

In the module, there is the require () method, which accepts the module designator and is used to introduce a module.

1 Main.js 2 -----------------------------------------3var a = 5; 4 var b = 4; 5 var addTwo = require ("./addtwo"); // This parameter is indicated by the module 6   7 Console.log (Addtwo.add (A, b)); // 9

The COMMONJS specification loading module is synchronous, that is, only the loading is complete before the subsequent operation can be performed. The AMD specification is a non-synchronous loading module that allows you to specify a callback function. Since node. JS is primarily used for server programming, the module files are generally present in the local hard disk, so it is faster to load and do not consider the way of non-synchronous loading, so the COMMONJS specification is more applicable.

1.2 AMD Specification Examples

AMD is the abbreviation for "Asynchronous module definition", meaning "async module definition".

AMD has designed a simple write module API:
define ([id], [dependencies], factory);
The first parameter ID is a string type that represents the module ID, which is an optional parameter. If it does not exist, the module identity should be defined by default as the identity of the requested script in the loader. If present, then the module ID must be a top-level or an absolute identity.
The second parameter, dependencies, is an array literal that the current module relies on to identify the module that has been defined by the module.
The third argument, factory, is a function or an object that needs to be instantiated.

1. HTML code

1 index.html2 ---------------3  <!DOCTYPE HTML>4  <HTML>5  <Head>6     <title></title>7     <!--introduction of Require.js -8     <Scripttype= "Text/javascript"src= "Http://cdn.bootcss.com/require.js/2.3.3/require.js"></Script> </Head>9   <Body>Ten     <Scripttype= "Text/javascript"src= "Main.js"></Script> One   </Body> A  </HTML>

2. JS Code

1 Myname.js2---------------3 //Defining Modules4Define (' MyName ', [],function () {5   return' My name is Toto_li. '6 })7 8 Yourname.js9---------------Ten //Defining Modules OneDefine (' YourName ', [],function () { A   return' Your name is Boke. ' -})

Load Module : The AMD Modular specification uses a global or local require function implementation to load one or more modules, all of which have a callback function after the module has completed loading.

Require ([module], callback):

[Module]: is an array in which the member is the module to be loaded;
Callback: Is the callback function after module load is complete.

1 Main.js 2  ---------------3  //  call module 4  require ([' myName ', ' yourName '),  function  (myname,yourname) {5   console.log (myName)6    Console.log (yourName) 7  })

1.3 CMD Specification Example

CMD is the normalized output of the module definition in the SEAJS process.

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

    1 //AMD2define (['./a ', './b '),function(A, b) {3    4       //rely on the beginning to write well5 a.test ();6 b.test ();7   });8    9   //CMDTenDefinefunction(Requie, exports, module) { One        A      //Reliance can be written near -      varA = require ('./a ')); - a.test (); the        -      ... -      //Soft Dependency -      if(status) { +        -          varb = Requie ('./b ')); + b.test (); A      } at});

AMD, CMD, COMMONJS specification

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.