What is CommonJs?
**CommonJs** is a modular specification for the JavaScript language that is typically used on server-side Nodejs. The project is composed of multiple modules, and the call between modules requires the same standard API for each module, so that there will not be so much learning cost in the process of use, and it is clustering for a single module.
In the CommonJs modularity specification, each file is a module, with its own independent scope, variables, methods, etc., invisible to other modules. The CommonJS specification states that within each module, the module variable represents the current module. This variable is an object whose exports property (module.exports) is the interface to the outside world. Loading a module loads the module.exports property of that module. The require method is used to load modules.
CommonJS benefits include:
The code can be used to run Wells in node.js environments, for example to do isomorphic applications:
Many third-party modules released through Npm adopt the CommonJS specification.
The disadvantages of CommonJS are:
Such code cannot run directly in a browser environment and must be converted to standard ES5 using tools.
CommonJSl can only be exported through exports. XX = XX. CommonJS2 added module. Exports = XX on the basis of CommonJSl. CommonJS usually refers to CommonJS2.
Modular specifications project to bring huge benefits in business complex, module of many big projects, developers all follow the same rules to develop their own module, they constrain module by specification definition, you don’t need too much communication or a large amount of documents to illustrate their own modules using rules, so thousands of module production, and can be easy to use. Not only does it make modules look standardized, but it also plays an important role in collaborative development and community dissemination.
What is UMD?
UMD is called the Universal Module Definition. It also follows the trend towards a big front end, which allows the same code module to run at run time or compile time in projects using CommonJs, CMD or even AMD. In the future, the same JavaScript package running on the browser side, the server side, and even the APP side will only need to follow the same script.
It does not have its own specification, is a combination of CommonJs, CMD, AMD specifications in one, let’s look at the concrete implementation of it:
It is not hard to find that it detects the current environment and module definition when defining modules, and converts various modular definition methods into the same writing method. Its appearance is also the product of the development of front-end technology, front-end in the realization of cross-platform progress, UMD specification will be browser side, server side and even APP side are unified, of course, it may not be the best modular way in the future, In the future, languages with advanced syntax such as ES6+, TypeScript, and Dart will replace these solutions.
What is [CMD]
CMD, called the Common Module Definiton, is similar to the CommonJs modularity specification, but runs on a browser. We learned about the benefits of modularity in the CommonJs article. It is developed with the increasing complexity of front-end business and architecture applications, and SeaJS from Tao Bao Yu Bo is its implementation.
The CMD specification is kept as simple as possible and has great compatibility with CommonJS ‘Modules specification. Modules written by the CMD specification can be easily run in Node.js. In the CMD specification, a module is a file. The format is as follows:
Specific usage is as follows:
The CMD specification, with its simple, asynchronous loading scripts, friendly debugging, and Nodejs compatibility, does give us a better way to manage modules during development.
What is AMD?
AMD is called the Asynchronous Module Definition specification, and it is a superset of the CommonJs modularity specification, but runs on browsers, and we learned about the benefits of modularity in the CommonJs article. AMD is true to its name. Modules are loaded asynchronously, taking advantage of the browser’s ability to make concurrent requests and making the module dependent process less blocked. RequireJs is an implementation of the AMD modular specification.
AMD, as a specification, just defines its syntax API, not its implementation. The AMD specification is as simple as a single API, the define function:
Specific usage is as follows:
It may look similar to CMD, but there are some implementation differences, and each has its own strengths and weaknesses. We will compare these modular specifications in particular in the future.