Commonjs,amd,cmd differences

Source: Internet
Author: User

   ZCCST reprint    Learn to compare dizzy, again see COMMONJS,AMD, cmd as if still not fully clear, today again tidy up:  COMMONJS is used in the server side, synchronous, such as NODEJS AMD, CMD is used in browser-side, asynchronous, such as Requirejs and seajs  which, AMD first proposed, the CMD is based on COMMONJS and AMD is proposed.    Why is it dizzy? It is because it is useless or used too little. It's no good to look at the article.     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.    Example:  //foobar.js  //private variable  var test = 123;  //Public method  function Foobar () {& nbsp;     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;  //require method reads the JS file by default, so you can omit the js suffix  var test = require ('./ Boobar '). foobar;&Nbsp; test.bar (); The    COMMONJS load module is synchronous, so only the loading is complete to perform the subsequent operation. 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.    AMD (asynchromous module definition)   AMD is a normalized output of the module definition in the promotion process of Requirejs    AMD loads the module asynchronously. Its modules support various types of modules such as Object function constructor string json.    applicable AMD specifications apply the Define method definition module.   //the dependency through an array, the callback function passes through the parameter to the dependent  define ([' SomeModule1 ', ' someModule2 '], function (SomeModule1, someModule2 ) {      function foo () {        ///someing          somemodule1.test ();    }       return {foo:foo} }); The  AMD specification allows the output module to be compatible with the COMMONJS specification, when the Define method is as follows:   define (function (Require, Exports, module) {          var reqmodule = require ("./somemodule");      RequmoDule.test ();          Exports.asplode = function () {         //someing    } });     cmd   cmd is seajs in the promotion process of the module definition of the standardized output   cmd and AMD the difference between the following:   1. For dependent modules AMD is executed ahead of time, and CMD is deferred for 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).   2.cmd relies on proximity, AMD advocates reliance on pre-built.  //amd define (['./a ', './b '), function (A, B) {     //reliance on writing well at the beginning       A.test ();     b.test ();  });   //cmd define (function (Requie, exports , module) {         //dependency can be written near      var a = require ( './a ');     a.test ();          ...     //Soft dependencies      if (status) {               var b= Requie ('./b ');         b.test ();    } });   Although AMD also supports CMD notation, relying on the predecessor is the default module definition for official documents. The   3.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. The main differences between   SEAJS and Requirejs are explained here.

Commonjs,amd,cmd Differences

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.