AMD, CMD, and COMMONJS specifications

Source: Internet
Author: User

The COMMONJS specification Commonjs is a project created to target a JavaScript ecosystem outside the browser environment, such as in a server and desktop environment. The COMMONJS specification is a modular form that is defined to address the scope of a JavaScript problem and allows each module to execute in its own namespace. The main content of this specification is: The module must be exported through Module.exports to external variables or interfaces, through require () to import the output of other modules to the current module. Example:
  modulea.js  module.exports = function (value) {      return value * 2;  }  

 

Moduleb.js  var multiplyBy2 = require ('./modulea ');  

COMMONJS is a synchronous loading module, but there is also a browser-side implementation, the principle is that all modules are defined and indexed by the ID, so that the browser can be parsed

node. js on the server side follows the COMMONJS specification. The core idea is to allow the module to synchronously load other modules to be relied on by the Require method, and then export the interfaces that need to be exposed via exports or module.exports

Require ("module");  Require (".. /file.js ");  Exports.dostuff = function () {};  Module.exports = somevalue;

  

Advantages:
    • Server-side ease of reuse
    • There are nearly 20w module packages in NPM
    • Simple and easy to use
Disadvantages:
    • Synchronous modules do not fit in a browser environment, synchronization means blocking loading, and browser resources are loaded asynchronously
    • Cannot load multiple modules in parallel without blocking
Details: http://zhaoda.net/webpack-handbook/module-system.html http://javascript.ruanyifeng.com/nodejs/module.html#to The C4AMDAMD specification actually has only one primary interface define (id,dependencies,factory), which specifies all of the dependent dependencies when declaring the module, and also as a formal parameter to the factory, For dependent modules to be executed ahead of time, relying on the predecessor
Define ("module", ["Dep1", "DEP2"], function (d1, D2) {    return someexportedvalue;  });  Require (["module", ".. /file "], function (module, file) {/* ... */});  

  

Advantages:
    • Suitable for asynchronous loading in a browser environment
    • Loading multiple modules in parallel
Disadvantages:
    • Improve development costs, code reading and writing is difficult
    • Not conforming to the common mode of modular thinking is a kind of compromise realization
Details: Http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.htmlCMD

The CMD specification is similar to AMD and is kept as simple as possible, and is compatible with the COMMONJS and Nodejs modules specifications.

Define (function (Require, exports, module) {    var $ = require (' jquery ');    var Spinning = require ('./spinning ');    exports.dosomething = ...    Module.exports = ...  })  

  

Advantages:
    • Rely on the nearest, deferred execution
    • It's easy to run in node.
Disadvantages:
    • Depending on SPM packaging, the load logic of the module is biased
Implementation: SEAJS

Turn from 51970567

AMD, CMD, and Commonjs Specifications (RPM)

Related Article

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.