CommonJS specification and AMD / CMD specification summary

Reprinted: https://www.jianshu.com/p/7c7fc199b7aa

  • The CommonJS standard loading module is synchronous, and only after the loading is completed can the following operations be performed.
  • The AMD specification is for asynchronously loading modules, allowing callback functions to be specified.
    Since Node.js is mainly used for server programming, module files are generally already on the local hard disk, so it loads faster, and does not need to consider the asynchronous loading method, so the CommonJS specification is more applicable. However, if it is a browser environment, to load the module from the server side, you must use the asynchronous mode, so the browser side generally adopts the AMD specification.

CommonJSIn the specification module, exportsandrequire

  • Each file is a module and has its own scope. Inside each module, the modulevariable represents the current module and is an object, and its exportsattributes (ie module.exports) are external interfaces.
  • module.exportsThe attribute indicates the current output interface of the module, and other files loading the module actually read the module.exportsvariables.
  • For convenience, Nodeprovide a exportsvariable for each module to point to module.exports.
let exports = module.exports;
  • requireThe command is used to load the module file.

Examples of use:

  // name.js 
  exports.name = function () { return '李婷婷'}; // export 
  // getName.js 
  let getName = require ('name'); // import

Note: You cannot directly exportspoint the variable to a value, because this is equivalent to cutting off the exportsconnection with module.exports:

exports = function(x){console.log(x)}

If the external interface of a module is a single value, the exportsoutput cannot be used , only the module.exportsoutput.

AMD / CMD specifications

  • AMDIt is RequireJSthe standardized output of module definition in the promotion process.
    CMDIt is SeaJSthe standardized output of module definition in the promotion process.
  • For the dependent modules, it AMDis executed in advance and CMDdelayed. But RequireJSfrom the 2.0beginning, it can be changed to perform delay (depending on the wording handled differently). CMDRespect as lazy as possible.
  • CMDRespect dependence on the nearest, and AMDrespect dependence on the front.
// CMD 
define ( function (require, exports, module) {    
   let a = require ('./ a' ); 
   a.doSomething (); 
   · 
   let b = require ('./ b'); // depend You can write    
   b.doSomething (); 
   ... 
}) 

// AMD recommends 
define (['./ a', './b'], function (a, b) { 
   // dependence must start Just write     
  a.doSomething ()    
  ... 
  b.doSomething ()    
  ... 
})

Although it AMDalso supports CMDthe wording and also supports passing requireas a dependency, RequireJSthe author defaults to the above wording, which is also the default module definition in the official document.

  • AMDThe APIdefault is a strict distinction between multiple uses, and CMDa APIsingle responsibility.
    For example AMD, the requireglobal requireand local are requirecalled require. CMDHere, there is no global require, but according to the completeness of the module system, it is provided seajs.useto realize the loading start of the module system. CMDHere, each APIis simple and pure.

Expand
all current engines have not yet achieved import, in nodethe use of babelsupport ES6, it is only to ES6transcode ES5and then executed, importsyntax will be transcoded to require. This is why it is used when the module is exported, module.exportsand it importstill works when the module is imported , because in essence, it importwill be transcoded requireto execute.

What is the difference between AMD and CMD in the reference document
CommonJS specification
?
Node does not understand require and import, you will be miserable

Guess you like

Origin www.cnblogs.com/web-record/p/12705808.html