CommJS
CommonJS(www.commonjs.org) is a widely used Javascript modularization specification. The core idea is to synchronously load dependent modules through require method; The popularity of the CommonJS specification was helped by the adoption of this approach by Node.js, which was later introduced into web development.
Advantages:
1. Code can be reused and run in node.js. 2. Many third-party modules distributed through NPM adopt the CommonJs specificationCopy the code
Disadvantages:
1. It cannot run directly in the browser environment, and must be converted to the standard ES5 2 by using a tool. Synchronous loading modules are more likely to be used on servers, where many resource files are localCopy the code
CommonJS usually refers to CommonJS2. Exports in CommonJS1 is actually a reference to module.exports.
Exports =XX is not recommended. Export only through exports.xx = XX;
CommonJS2 added module. Exports = XX on the basis of CommonJSl.
commonJS1 eg:
Exports ['child10'] =' child10' exports. Child11 ='childe11' parent // add a layer of namespace child1.child11Copy the code
commonJS2 eg:
Exportmodule. exports = function export20 (){console.log(' export20... ')}; Const Child2 = require('./child2.js'); Const child31='child31'; const child31='child31'; const child32=function (){console.log('child32... ')}; Module. Exports = {child31: child31 child32: child32} parent. Js import const {child31} = the require (". / child3. Js'); const child32=require('./child3.js').child32; The parent. Js output child31 (); child32();Copy the code
Print the console. The log (module);
All modules are an instance of the constructor Module, which contains the properties shown above. If there is no parent module and the current module is called directly, the parent attribute is null and the ID attribute is a dot. The filename attribute is the absolute path to the module, and the Path attribute is an array containing the possible locations of the module
AMD
AMD (en.wikipedia.org/wiki/Asynch… It is also a JavaScript modularization specification that differs from CommonJS in that it takes an asynchronous approach to loading dependent modules. The AMD specification is mainly used to solve the modularity problem for the browser environment, the most representative implementation is requirejs (requirejs.org)
AMD import and export code is as follows:
Advantages:
Can run directly in the browser without converting code; Asynchronously loaded dependencies: Multiple dependencies can be loaded in parallel: code can run in both browser and Node.js environments.Copy the code
Disadvantages:
JavaScript runtime environment does not have native support for island A. , need to enter the implementation of the AMD library before normal useCopy the code
ES6 modular
ES6 modularization is a JavaScript modularization specification proposed by THE international organization for Standardization ECMA, which realizes modularization at the language level. Both browser vendors and Node. Have announced their intention to support the specification natively. It will gradually replace CommonJS and AMD specifications as a common modular solution for browsers and servers.
Features:
The ES6 module is the ultimate modular solution, but its disadvantage is that it cannot run directly in most JavaScript environments, and must be converted to standard ES5 by tools to run properly. Each module is loaded only once and executed only once, loading the same file repeatedly and reading directly from memory; Variables declared in each module are local variables and do not pollute the global scope. Export modules and import modulesCopy the code
Export can only be function, class, var, let, const, default, {}. The function of export is to add attributes to the current module object for later import to other modules.
As rename: