<转载> JAVASRIPT Module Specification-Introduction to AMD Specification and CMD specification __java

Source: Internet
Author: User

Reprint Source: http://blog.chinaunix.net/uid-26672038-id-4112229.html

Javasript ModularBefore understanding the AMD,CMD specification, still need to first come to understand what is modular, modular development.
Modularity refers to the systematic decomposition of a problem in the process of solving 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 decompose into manageable modules that are more reasonable and maintainable for code structures. It can be imagined that a huge system code, being integrated and optimized to be segmented into a logically strong module, is a kind of meaning for software. For the software industry: decoupling software system complexity, so no matter how large the system, can also be management, development, maintenance become "reasonable to follow."
There are some professional definitions for modularity: Modularity is a property of a software system that is decomposed into a group of highly cohesive, low coupling modules. So in the ideal state we only need to complete their own part of the core business logic code, other aspects of dependence can be directly loaded by the person has been written to use the module.
First of all, since the modular design, then as a modular system must have the capacity: 1.     Defines the encapsulated module.     2. Define the dependencies of new modules on other modules. 3. The introduction of other modules can support.
Well, thinking has, then always have something to establish a modular system of norms, or various modules will only be loaded in the way the bureau will stir more confusion. So in JavaScript There are some unconventional module Development mode Specification COMMONJS Module specification, AMD (asynchronous module definition), CMD (Common module definition) and so on.
AMD and Requirejs AMDAsynchronous module definition, in the vernacular is asynchronous module definitions, for Jser, asynchronous is no longer familiar with the word, all the modules will be loaded asynchronously, the module load does not affect the following statement to run. All statements that depend on certain modules are placed in the callback function.
The AMD specification defines a free variable or a function that is a global variable define.

Define (ID, dependencies, Factory); AMD specification Https://github.com/amdjs/amdjs-api/wiki/AMD The first parameter ID is a string type, representing the module identity, which is an optional parameter. If not, the module identification should be defined by default as the identity of the requested script in the loader.     If present, the module identification must be either a top-level or an absolute identity.     The second parameter, dependencies, is the literal amount of an array that is dependent on the current module and is identified by the module definition. 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 () {
return Beta.verb ();
Or
return require ("beta"). verb ();
}
}); An asynchronous module that returns the literal volume of an object
define (["alpha"], function (Alpha) {
return {
Verb:function () {
return Alpha.verb () + 1;
}
}
}); Non-dependent modules can be defined directly by using object literals
Define ({
Add:function (x, y) {
return x + y;
}
} ); Similar and commonjs method definitions
Define (function (Require, exports, module) {
var a = require (' a '),
b = require (' B ');

Exports.action = function () {};
} );

Require (); Require API introduction Https://github.com/amdjs/amdjs-api/wiki/require
The Require function in the AMD specification differs from the require in the general Commonjs. Because dynamic detection dependencies make the load asynchronous, the demand for require based on callbacks is strong.

Local and global require local require need to be passed into require in the Define factory function in AMD mode.
define ([' Require '], function (require) {
// ...
} );
Or

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.