Introduction to JavaScript AMD Specification (i) __java

Source: Internet
Author: User
AMD is the abbreviation for "Asynchronous module definition", meaning "asynchronous modular definition". AMD defines that the modules we use are loaded asynchronously, so we want to place the code fragment of the dependent module in a callback function that triggers the callback function when the asynchronous loading module is complete (that is, when this module is available) the AMD libraries commonly used are require.js, sea.js Click here to have an AMD implementation of my own sample level
The following two APIs (1) require ([module], callback) are defined in the AMD standard;    (2) define ([depends], callback); The Require interface is used to load a series of modules, define interfaces to define and expose a module
In AMD, our JS code is placed in the require callback, the Require function helps us load the dependent modules and the processor's deep dependencies (dependent dependencies, and so on) and, after all dependencies have been loaded, takes the module as a callback function and invokes the callback function. See the following scenario our JS code needs B,C module. B module is not dependent on any module, C module depends on D module
Require ([B,c],function (b,c) {       
     var A = {};
     A.say = B.say;
     A.run = C.run;
     return A;
});

B.js
define ([], function () {
     var B = {};
     B.say = function () {
          console.info (' Say hello! ');
     }
     return B;
});

C.js
define ([d], function (d) {
     var c={};
     C.run = function () {
          console.info (d.name + ' free running! ');
     }
     return C;
})

D.js
define ([],function () {
     var d={' name ': ' Songzheng '};
     return D;
});
First assume that all modules are not loaded, the entire load process is as follows (1) code relies on B, C module, call require load B, C module (2) b module does not depend on any module when the load succeeds, callback Factoryb, install B module (3) When loading C module found C module relies on the D module so call require Load D module (4) d module does not rely on any module, after the successful loading, callback Factoryd load install D module (5) C dependent on the D module load succeeded, callback FACTORYC, install C module (6) All dependencies installed, execute callback function
I added some log printing to my AMD implementation steps to see the module loading process more clearly
<script type= "Text/javascript" >
     require ([' Iris ', ' cookie '], function (Iris, cookie) {
          console.info ( Iris);
          Console.info (Cookie);
</script>



Related Article

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.