Javasript Modular-AMD specification and CMD specification

Source: Internet
Author: User

JavaScript modularity

Before understanding the AMD,CMD specification, let's start with a simple understanding of what is modular and modular development.

Modularity refers to the systematic decomposition of a problem in order to solve a complex problem or a series of mixed problems, according to a sort of thinking. Modularity is a way of dealing with complex systems that break down into manageable modules that are more logical and maintainable in code structures. Imagine a huge system code, how meaningful it is for software to be integrated and optimized to be segmented into highly logical modules. For the software industry: The complexity of decoupling software systems makes it possible to manage, develop, and maintain a "rational and predictable" system, no matter how large.

Some of the modules are defined as: Modularity is a property of a software system that is decomposed into a set of high-cohesion, low-coupling modules.

      since it is a modular design, the ability to act as a modular system:
    1. Defines the encapsulated module.
    2. Define the dependencies of the new module on other modules.
    3. The introduction of other modules can be supported.

Thought has, there is always a little something to build a modular system of norms, otherwise all kinds of module loading mode will only make the bureau more chaotic. There are some specifications of non-traditional module development methods in Avascript COMMONJS module specification, AMD (asynchronous module definition), CMD (Common module definition)

AMD

Asynchronous module definition, in vernacular speaking is the asynchronous module definition, for Jser, async is no longer familiar with the word, all modules will be loaded asynchronously, module loading does not affect the subsequent statement run.     All statements that depend on certain modules are placed in the callback function. The AMD specification defines a function that is either a free variable or a global variable define.

Define (ID?, dependencies, Factory);

AMD Spec Https://github.com/amdjs/amdjs-api/wiki/AMD The first parameter ID is a string type, which represents the module ID, which is an optional parameter. If it does not exist, the module identity should be defined by default as the identity of the requested script in the loader.    If present, then the module ID must be a top-level or an absolute identity.    The second parameter, dependencies, is an array literal that the current module relies on to identify the module that has been defined by the module. The third argument, factory, is a function or an object that needs to be instantiated. Create modules identified as alpha, dependent on require, export, and modules identified as Beta

Define ("Alpha", ["require", "exports", "beta"], function (require, exports, beta) {    export.verb = function () {        re Turn Beta.verb ();        Or:        return require ("beta"). verb ();    }});
An asynchronous module that returns the literal of an object
define (["alpha"], function (Alpha) {    return {        verb:function () {            return Alpha.verb () + 1;        }}    );
No-dependency modules can be defined directly using object literals
Define ({    add:function (x, y) {        return x + y;    }});

Similar to the CommonJS method definition

Define (function (Require, exports, module) {    var a = require (' a '),          B = require (' B ');    Exports.action = function () {};} );

require ();

Require API describes the Https://github.com/amdjs/amdjs-api/wiki/require in the AMD specification require function differs from the require in general Commonjs. Because dynamic detection of dependencies makes loading asynchronous, there is a strong demand for callback-based require.

require of local and global

Local require need to pass in the require in the Define factory function in AMD mode.
define ([' Require '], function (require) {  //...}); O R:define (function (Require, exports, module) {  //...});
Local require require other specific APIs to implement. The global require function is the only variable in the global scope, like define. Global require is not a specification requirement, but if you implement a global require function, then it needs to have the following qualification as the local require function: 1.    The module identity is considered absolute, not relative to the other module identity. 2. Only in asynchronous cases, require's callback method is used as an interactive operation.    Because he could not load the module from the top level with require (String) in the case of synchronization. Dependency-dependent APIs will start the module loading. If you need to have multiple, interoperable loaders, the global reqiure should be loaded with the top-level module instead. 
Require (String) define (function (require) {    var a = require (' a ');//Load Module A}); require (Array, function) define (functio N (require) {    require ([' A ', ' B '], function (b) {///load module a B using        //dependent on the run code of a B module    }); Require.tourl (URL) define (function (require) {    var temp = Require.tourl ('./temp/a.html ');//Load page});

Amdjs's API Https://github.com/amdjs/amdjs-api/wiki

where is the difference between AMD and CMD?       read the above Amd,requirejs and CMD, Seajs's brief introduction will be a little vague, the total feeling is more similar. Because like Requirejs it is not just pure amd inherent ideas, it is also the idea of a CMD specification, but is recommended AMD Standard way, SEAJS is the same.       Below is Yuber's explanation for the difference between AMD and CMD:      AMD is the normalized output defined by Requirejs in the promotion process.     cmd is the normalized output of the module defined by SEAJS in the process of generalization.       similar to the CommonJS modules/2.0 specification, is bravojs in the promotion process of the module definition of standardized output still a lot??       These specifications are intended for the modular development of JavaScript, especially on the browser side.      at present, the implementation of these specifications can achieve the purpose of browser-side modular development.        difference:      1. For dependent modules, AMD is executed ahead of time, and CMD is deferred execution. However, starting from 2.0, Requirejs is also changed to be deferred (depending on the way it is written and handled differently). CMD respected as lazy as possible.    2. The CMD is highly dependent on the nearest, AMD is highly dependent on the predecessor.
Cmddefine (function (Require, exports, module) {    var a = require ('./a ')    a.dosomething ()    //Omit 100 rows    here var B = require ('./b ')//dependency can be written near    b.dosomething ()    ///...}) AMD defaults to the recommended define (['./a ', './b '), function (A, B) {//dependencies must be written at the beginning    a.dosomething ()    //100 lines omitted here    B.dosomethin G ()    //...})

3. AMD's API defaults to one when multiple uses, the CMD API is strictly differentiated, advocating a single responsibility. For example, AMD, require global require and local require, are called require. In CMD, there is no global require, but according to the completeness of the module system, provide seajs.use to realize the loading and starting of the module system. CMD, each API is simple and pure.

4. There are also some details of the difference, the specific definition of the specification is good, not much to say.

In addition, SEAJS and Requirejs differences, you can refer to: https://github.com/seajs/seajs/issues/277

Javasript Modular-AMD specification and CMD specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.