Today I learned a bit about JavaScript's modular programming and wrote a simple example.
———————————————— This is my first technical little blog post, I hope to communicate with you ————————————————
I'm going to make a list of two parts:
1. File map
2. Source CodeDemo.html:
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 <title> Module Loading simple example </title>6 <script data-main= "main" type= "Text/javascript" src= "Require.js"></script> 78 </Head>9 <Body>Ten Empty page One </Body> A </HTML>
A.js
var function (numa,numb) { return NumA + NumB;}
B.js
// Subtraction var function (numa,numb) { return NumA- NumB;} // Division Object function Glindiv (divisor) { this. Divisor = divisor; This function (num) { returnthis. divisor; }}
C.js
// multiplication var function (num,ples) { return num * ples;} // example Chemical control method for image New /* = = = This depends on the Glindiv object defined in b.js = = */
Main.js
//Module Load Path configurationRequire.config = { "A": "A", "B": "B", "C": "C"}//The order in which the modules are loaded and the-----------of the callback function (where "C" must be after "B", because the "//instance Chemical control" in "C" for "//Division Object" in "dependent" B ")Require (["A", "B", "C"],function(a,b,c) {varNUM01 = 10; varNUM02 = 20; varPels = 2; //call the function in the loaded module to perform the Operation varTol =rsub (Radd (NUM01,NUM02), Rmul (Num01,pels)); Alert (Mydiv.rdiv (tol));//pop-up window test });
Require.js (Tool library, own Baidu)
Conclusion: Welcome all students to exchange guidance, especially hope to get some good blog presentation and display ways of advice!
Require.js "Modular Programming"-simple example of module loading