Introduction to JavaScript AMD Specifications (I)

Source: Internet
Author: User

AMD is the abbreviation for "Asynchronous module definition", meaning "async module definition". AMD defines that the modules we use are all asynchronously loaded, so we're going to put the code fragment of the dependent module in a callback function, and when the asynchronous load module is complete (that is, when the module is available), trigger the callback function. The commonly used AMD libraries are require.js, Sea.js Click here for a sample level of my own AMD implementation
The AMD standard defines the following two APIs (1) require ([module], callback);(2) define ([depends], callback);  The Require interface is used to load a series of modules, and the Define interface is used to define and expose a module
In AMD our JS code is placed in the require callback, the Require function to help us load the dependent module and the processor deep-seated dependencies (dependent dependencies, etc.) and after all dependencies loaded, the module as a callback function of the parameters, and call the callback function. Look at the following scenario our JS code requires B,C module. b modules are not dependent on any modules, C modules depend on D modules
Require ([B,c],function (b,c) {            var A = {};     A.say = B.say;     A.run = C.run;     return A;}); B.jsdefine ([], function () {     var B = {};     B.say = function () {          console.info (' Say hello! ');     }     return B;}); C.jsdefine ([d], function (d) {     var c={};     C.run = function () {          console.info (d.name + ' free running! ');     }     return C;}) D.jsdefine ([],function () {     var d={' name ': ' Songzheng '};     return D;});
Assuming that all modules are not yet loaded, the entire loading process is as follows (1) The code depends on the 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 the C module, it is found that C module relies on D module to call require Load D module (4) d module does not rely on any module, after loading successfully, callback Factoryd load installation D module (5) C dependent D module load succeeded, callback FACTORYC, install C module (6) All dependent installations complete, execute callback function
I added some log printing to my AMD implementation steps to see more clearly the loading process of the module
<script type= "Text/javascript" >     require ([' Iris ', ' cookie '], function (Iris, cookie) {          console.info ( Iris);          Console.info (Cookie);     }); </script>



Introduction to JavaScript AMD Specifications (I)

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.