Specifications of ipvrip-amd specifications and CMD specifications

Source: Internet
Author: User
Tags define definition


Modular architecture
Before learning about AMD and CMD specifications, you still need to first briefly understand what is modular and modular development?
Modularization refers to the systemic decomposition of a problem according to a classification of thinking when solving a complex problem or a series of Mixed Problems. Modularization is a way to process complicated systems into manageable modules with a more reasonable code structure and higher maintainability. It can be imagined that there is a great significance for software when a huge system code is integrated and optimized and divided into logic modules. For the software industry: decoupling the complexity of software systems makes management, development, and maintenance "rational" no matter how large a system is ".
Some other major modules are defined as: modularization is the property of a software system, which is divided into a group of high cohesion and low coupling modules. Ideally, we only need to complete some of our core business logic code. Other dependencies can be directly loaded and used by the modules already written.
First of all, since it is a modular design, the required capabilities as a modular system: 1. Define the encapsulated modules. 2. Define the dependency of the new module on other modules. 3. Support the introduction of other modules.
Well, if you have an idea, there is always something to do to build a modular normative system. Otherwise, all kinds of module loading methods will only confuse the situation. In JavaScript, there are some non-traditional Module development methods that regulate CommonJS Module specifications, such as AMD (Asynchronous Module Definition) and CMD (Common Module Definition.

AMD and RequireJSAMDAsynchronous Module Definition are defined by asynchronous modules in white words. For JSer, Asynchronization is a word that is no longer familiar with, and all modules will be asynchronously loaded, module loading does not affect subsequent statements. All statements that depend on certain modules are placed in the callback function.
AMD norms define a function of free variables or global variables define.

Define (id ?, Dependencies ?, Factory );

AMD specifications: https://github.com/amdjs/amdjs-api/wiki/amdthe first parameter id is a string type, which indicates the module id and is an optional parameter. If this parameter does not exist, the module identifier should be defined as the identifier of the requested script in the loader by default. If yes, the module ID must be a top-level or absolute ID. The second parameter, dependencies, is an array literal that the current module depends on and has been defined by the module. The third parameter, factory, is a function or object that needs to be instantiated.
Create a module that identifies as alpha, depending on require, export, and a module that identifies as beta

 
 
  1. define("alpha", [ "require", "exports", "beta" ], function( require, exports, beta ){
  2. export.verb = function(){
  3. return beta.verb();
  4. // or:
  5. return require("beta").verb();
  6. }
  7. });


An asynchronous module that returns the object literal
 
 
  1. define(["alpha"], function( alpha ){
  2. return {
  3. verb : function(){
  4. return alpha.verb() + 1 ;
  5. }
  6. }
  7. });


The no-dependency module can be defined using the object literal.
 
 
  1. define( {
  2. add : function( x, y ){
  3. return x + y ;
  4. }
  5. } );


Similar to CommonJS
 
 
  1. define( function( require, exports, module){
  2. var a = require('a'),
  3. b = require('b');

  4. exports.action = function(){};
  5. } );


Require ();

Require API introduction https://github.com/amdjs/amdjs-api/wiki/require
The require function in AMD specifications is different from the require function in common CommonJS. Because the dependency is dynamically detected to make loading asynchronous, there is a strong demand for callback-based require.

Local and global require

Local require must be passed in to the define factory function in AMD mode.
 
 
  1. define( ['require'], function( require ){
  2. // ...
  3. } );
  4. or:
  5. define( function( require, exports, module ){
  6. // ...
  7. } );



Local require must be implemented by other specific APIs. The global require function is the only variable in the global scope, like define. Global require is not required by the standard. However, if you implement the Global require function, it must have the same limits as the local require function: 1. the module ID is regarded as absolute, rather than corresponding to another module ID. 2. The require callback method is used as an interactive operation only in asynchronous mode. Because it is impossible for him to load modules from the top layer through require (String) during synchronization.
Dependent APIs start module loading. If multiple loaders are required for interoperability, the global reqiure should be replaced by the top-level modules.

 
 
  1. Require (String)
  2. Define (function (require ){
  3. Var a = require ('A'); // load module
  4. });

  5. Require (Array, Function)
  6. Define (function (require ){
  7. Require (['A', 'B'], function (a, B) {// used to load module a B
  8. // Depends on the Running code of module a B
  9. });
  10. });

  11. Require. toUrl (Url)
  12. Define (function (require ){
  13. Var temp = require. toUrl ('./temp/a.html'); // load the page
  14. });


API https://github.com/amdjs/amdjs-api/wiki of amdjs

RequireJS official website http://www.requirejs.org/APIhttp://www.requirejs.org/docs/api.html
RequireJS is a front-end tool library for modular management. Following AMD specifications, it is written by James Burke, founder of AMD specifications. Therefore, RequireJS cannot elaborate on AMD specifications at all.
The basic idea of RequireJS is to load all required or dependent modules through a function, and then return a new function (module ), all our business code about the new module is operated inside the function, and the loaded modules can be used without restriction.

 
 
  1. <script data-main='scripts/main' src='scripts/require.js'></script>

The main. js file in scripts is the specified Primary code script file. All dependent module code files will be asynchronously loaded and executed from this file.
Defined is used to define a module. RequireJS requires that each module be placed in an independent file. It is divided into independent modules and non-independent modules based on whether other modules are dependent. 1. independent modules, independent from other modules. Direct definition:
 
 
  1. define({
  2. method1: function(){},
  3. method2: function(){}
  4. });

It is also equivalent

 
 
  1. define(function(){
  2. return{
  3. method1: function(){},
  4. method2: function(){}
  5. }
  6. });

2. Non-independent modules, dependent on other modules.
 
 
  1. define([ 'module1', 'module2' ], function(m1, m2){
  2. ...
  3. });

Or:
 
 
  1. define( function( require ){
  2. var m1 = require( 'module1' ),
  3. m2 = require( 'module2' );
  4. ...
  5. });


After a brief look at the implementation method of RequireJS, its require implementation is simply to extract the function string and the module name after require and put it into the dependency.
Require method call module
When require calls a module, its parameters are similar to define.
 
 
  1. require( ['foo', 'bar'], function( foo, bar ){
  2. foo.func();
  3. bar.func();
  4. } );

After loading the foo and bar modules, execute the callback function to implement the specific process.
Of course, you can also perform the require call module in the define definition module as in the previous example.
 
 
  1. define( function( require ){
  2. var m1 = require( 'module1' ),
  3. m2 = require( 'module2' );
  4. ...
  5. });

Define and require are two definition modules. The method of calling the module is called AMD mode. The definition module is clear and does not pollute global variables. The dependency relationship is clearly displayed. AMD mode can be used in the browser environment and allow non-synchronous loading of modules, or dynamic loading of modules as needed.


In CMD and seaJSCMD, a module is a file in the format of define (factory );
The global function define is used to define a module. The factory parameter can be a function, an object, or a string. When factory is an object and a string, it indicates that the module interface is the object and a string.
Define the JSON data module:
 
 
  1. define({ "foo": "bar" });

Use the string to define the template module:
 
 
  1. define('this is {{data}}.');

When factory is a function, it indicates the module constructor. Execute the constructor to obtain the interface provided by the module.
 
 
  1. Define (function (require, exports, module ){
  2. // Module code
  3. });


Define (id ?, Deps ?, Factory); define can also accept more than two parameters. The string id is the module identifier, and the array deps is the module dependency:
 
 
  1. Define ('module', ['lele1', 'lele2'], function (require, exports, module ){
  2. // Module code
  3. });
Unlike AMD's standard usage.
Require is the first parameter of factory. Require (id); receives the module id as a unique parameter to obtain the interface provided by other modules:
 
 
  1. define(function( require, exports ){
  2. var a = require('./a');
  3. a.doSomething();
  4. });

Require. async (id, callback? ); Require is executed synchronously. The required asynchronous loading module can be loaded using require. async:
 
 
  1. define( function(require, exports, module) {
  2. require.async('.a', function(a){
  3. a.doSomething();
  4. });
  5. });

Require. resolve (id) can use the internal path mechanism of the module to return the module path without loading the module.
Exports is the second parameter of factory and is used to provide the Module Interface.
 
 
  1. Define (function (require, exports ){
  2. Exports. foo = 'bar'; // Attributes provided externally
  3. Exports. do = function () {}; // method provided externally
  4. });

You can also use return to directly provide an interface.
 
 
  1. Define (function (require, exports ){
  2. Return {
  3. Foo: 'bar', // Attributes provided externally
  4. Do: function () {}// method provided externally
  5. }
  6. });

It can also be simplified to the form of direct object literal:
 
 
  1. Define ({
  2. Foo: 'bar', // Attributes provided externally
  3. Do: function () {}// method provided externally
  4. });


As in nodeJS, the following method is incorrect:
 
 
  1. Define (function (require, exports ){
  2. Exports = {
  3. Foo: 'bar', // Attributes provided externally
  4. Do: function () {}// method provided externally
  5. }
  6. });


This is required
 
 
  1. Define (function (require, exports, module ){
  2. Module. exports = {
  3. Foo: 'bar', // Attributes provided externally
  4. Do: function () {}// method provided externally
  5. }
  6. });

You can add attributes to an input object reference. Once a new object is assigned, the reference of the object passed in will become invalid. At the beginning, exports is used as the module. A reference of exports exists, and all actions can run normally only on this reference. After a new object is assigned, the reference is disconnected, and exports is just a new object reference, it is meaningless for the factory, and errors will occur.
Module is the third parameter of factory. It is an object that stores some attributes and methods associated with the current module. Module. id is the unique identifier of a module. Module. uri gets the absolute path of the module based on the path parsing rules of the module system. Module. dependencies indicates the module dependency. Module. exports interfaces provided by the current module.

SeaJS Official Website
Core features of sea. js: 1. Follow the CMD specifications and write module code like NodeJS. 2. Automatic dependency loading, clear and concise configuration. Compatible with Chrome 3 +, Firefox 2 +, Safari 3.2 +, Opera 10 +, and IE 5.5 +.
Seajs. use is used to load one or more modules on the page.
 
 
  1. // Load a module
  2. Seajs. use ('./');
  3. // Load the module and execute a callback when loading is complete.
  4. Seajs. use ('./A', function (){
  5. A. doSomething ();
  6. });
  7. // Load multiple modules for callback
  8. Seajs. use (['./A','./B '], function (a, B ){
  9. A. doSomething ();
  10. B. doSomething ();
  11. });

The define and require usage methods are basically examples in the CMD specification.


What is the difference between AMD and CMD?
After reading the above AMD, requireJS, CMD, and seaJS simple presentations are a bit vague and generally similar. For example, requireJS is not just a pure AMD concept, but also a CMD standard idea. It only recommends AMD standard methods, as does seaJS.
The following is an explanation of the difference between AMD and CMD:
AMD is the standardized output of modules defined by RequireJS during promotion. CMD is the standardized output of modules defined by SeaJS during promotion.
Similarly, there is also the CommonJS Modules/2.0 specification, which is the standardized output of the module definition in BravoJS during the promotion process. How many ??
These specifications are designed for the modular development of JavaScript, especially on the browser side. At present, the implementation of these specifications can achieve the goal of browser-side modular development.
Differences:
1. AMD executes the dependent modules in advance, while CMD executes the tasks in a delayed manner. However, since 2.0, RequireJS can also be converted to delayed execution (depending on the Writing Method, the processing method is different ). CMD advocates as lazy as possible.2. CMD recommends proximity, while AMD recommends preference. Check the Code:
 
 
  1. // CMD
  2. Define (function (require, exports, module ){
  3. Var a = require ('./')
  4. A. doSomething ()
  5. // 100 rows are omitted here
  6. Var B = require ('./B') // The dependency can be written nearby.
  7. B. doSomething ()
  8. //...
  9. })

  10. // AMD recommended by default
  11. Define (['./A','./B '], function (a, B) {// dependencies must be written at the beginning
  12. A. doSomething ()
  13. // 100 rows are omitted here
  14. B. doSomething ()
  15. //...
  16. })

Although AMD also supports CMD writing and transmits require as dependencies, the author of RequireJS prefers the above writing by default and is also the default module definition writing in official documents.
3. By default, AMD APIs are used for multiple purposes. CMD APIs are strictly differentiated and have a single responsibility. For example, in AMD, require is divided into global require and local require, both of which are called require. In CMD, there is no global require. Instead, seajs. use is provided based on the completeness of the module system to load and start the module system. In CMD, every API is simple and pure.
4. There are some differences in details. Let's just look at the definition of this specification. In addition, SeaJS and RequireJS differences, can refer to: https://github.com/seajs/seajs/issues/277
This article summarizes the amd cmd specifications and briefly describes requireJS and seaJS. The description is more general. The extended reading below can help you better understand modularity and various specifications.

Additional reading:
AMD standard document https://github.com/amdjs/amdjs-api/wiki/AMD
Require interface document https://github.com/amdjs/amdjs-api/wiki/require of amdjs
Amdjs interface documentation https://github.com/amdjs/amdjs-api/wikirequirejs interface documentation http://www.requirejs.org/docs/api.html
Module System fuse module definition specification https://github.com/seajs/seajs/issues/242SeaJS API Quick Reference https://github.com/seajs/seajs/issues/266from CommonJS to Sea. jshttps: // github.com/seajs/seajs/issues/269

RequireJS and AMD specifications http://javascript.ruanyifeng.com/tool/requirejs.htmlcommonjsspecification

What are the differences between AMD and CMD? Http://www.zhihu.com/question/20351507
JavaScript modular development-CommonJS specification http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-commonjs
JavaScript modular development-AMD specification http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-amd

Modular Design http://baike.baidu.com/view/189730.htm
Modular http://baike.baidu.com/view/182267.htm


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.