This post was last edited by Summer on 2019-2-13 14:04
In 2009, American programmer Ryan Dahl created node.js project to use the JavaScript language for server-side programming.
This marks the birth of "Javascript modular programming". The complexity of the front-end is limited, and it is okay to have no modules, but on the server side, there must be modules to interact with the operating system and other applications, otherwise it cannot be programmed at all.
One of the most important ideas in node programming is modules, and it is this idea that makes large-scale engineering of JavaScript possible. Modular programming was popular in the JS world, and it was based on this, and then on the browser side, toolkits such as requirejs and seajs also appeared, so to speak, under the corresponding specification, require ruled all modular programming before ES6, even now, before ES6 module was fully implemented.
In CommonJS, the exposure module uses module.exports and exports, and many people don't understand why there are two exposed objects, which will be introduced later
In CommonJS, there is a global method require() that is used to load modules. Assuming there is a math module math.js, it can be loaded like this.
You can then call the methods provided by the module:
It is precisely because of the require method used by CommonJS that the require method used by AMD and CMD also used later to refer to the style of modules
AMD specification
With server-side modules, it's natural for everyone to want client-side modules. And it is best that the two are compatible, and a module can run on both the server and the browser without modification.
However, there is a major limitation that makes the CommonJS specification inapplicable to browser environments. Still the code from the previous section, if it runs in a browser, there will be a big problem
The second line math.add(2, 3) runs after the first line require('math'), so it has to wait for math.js load to finish. That is, if the loading time is long, the whole app will stop there and wait.
This is not a problem on the server side, because all modules are stored on the local hard disk and can be loaded synchronously, and the waiting time is the read time of the hard disk. However, for browsers, this is a big problem, because the modules are placed on the server side, and the waiting time depends on the speed of the Internet speed, which may take a long time, and the browser is in a "suspended death" state.
Therefore, browser-side modules cannot use "synchronous", but can only use "asynchronous". This is the background to the birth of the AMD specification.
AMD is an abbreviation for "Asynchronous Module Definition", which means "Asynchronous Module Definition". It loads the module asynchronously, and the loading of the module does not affect the operation of its subsequent statements. All statements that depend on this module are defined in a callback function that will not run until the loading is complete.
Modules must be defined with a specific define() function.
•ID: string, module name (optional) •dependencies: is the dependent module we want to load (optional), using the relative path. , note that it is an array format •factory: factory method, returns a module function If a module does not depend on other modules, it can be defined directly in the define() function.
If the module also depends on other modules, then the first argument of the define() function must be an array that indicates the module's dependencies.
When the require() function loads the above module, it will load Lib.js file first.
AMD also uses the require() statement to load the module, but unlike CommonJS, it requires two parameters:
The first parameter [module] is an array, and the members in it are the modules to be loaded; The second parameter callback is the callback function after the load is successful. If you rewrite the previous code into AMD form, it looks like this:
math.add() is not synchronized with math module loading, and the browser does not suspended animation. So obviously, AMD is more suitable for the browser environment.
Currently, there are two main Javascript libraries that implement the AMD specification :require.js and curl.js.
|