Home > Article > Web Front-end > What are the module specifications of JavaScript?
JavaScript module specifications include: 1. CommonJS specification; 2. AMD (Asynchronous Module Definition) specification; 3. CMD (Common Module Definition) specification; 4. UMD specification (a blend of AMD and CommonJS).
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There are three common JavaScript modular specifications, CommonJS, AMD (Asynchronous Module Definition), CMD (Common Module Definition)
Server: NodeJS Service: CommonJS specification, new version of Node You can also enable the ES6 Module function
Browser side: AMD specification and CMD specification are mainly used, which have been gradually replaced by ES6 Module
1. CommonJS specification
(1) Each file is a module, and each module has an independent scope. The variables in the file, Functions are private and cannot be used in other files (unless assigned to global) (2) Inside each module, the module variable represents the current module (3) The external interface of each file is the module.exports attribute (4) require is used to reference other modules. What is actually obtained is the module.exports attribute of other modules
2. AMD (Asynchromous Module Definition - Asynchronous module definition)
AMD is the standardized output of RequireJS module definition during the promotion process
Use
to define the module define(id?, dependencies?, factory) Load module require([module], callback)
##3. CMD (Common Module Definition - public module definition)
CMD is the standardized output of module definition during the promotion process of SeaJSUse
to define the module define(factory) Load module require(id)
4. UMD (a blend of AMD and CommonJS)
UMD first determines whether it supports Node.js Whether the module (exports) exists, if it exists, use the Node.js module mode. When determining whether AMD is supported (whether define exists), if it exists, use the AMD method to load the module.(function (window, factory) { if (typeof exports === 'object') { module.exports = factory(); } else if (typeof define === 'function' && define.amd) { define(factory); } else { window.eventUtil = factory(); } })(this, function () { //module ... });【Related recommendations:
The above is the detailed content of What are the module specifications of JavaScript?. For more information, please follow other related articles on the PHP Chinese website!