Some time ago, because some things this habit has fallen, now strive to pick up again.
Recently began to self-study requirejs, in order to better grasp, so write a self-understanding of the blog for reference.
Split Line-------------------------
First of all, learn requirejs, to know what is Requirejs.
The Requirejs is a very compact JavaScript module that can be run in JS and node environments under the AMD specification, and is commonly understood as a module loader.
I believe that the beginning of learning Requirejs of the JS code of the modularization has a preliminary understanding. And Requirejs mainly solves two major problems.
First. In the traditional modular package, it is necessary to be embarrassed into the purpose of introduction and the introduction of the order has a great rejection and annoyance.
<Scripttype= "Text/javascript"src= "A.js"></Script><Scripttype= "Text/javascript"src= "B.js"></Script><Scripttype= "Text/javascript"src= "C.js"></Script><Scripttype= "Text/javascript"src= "D.js"></Script><Scripttype= "Text/javascript"src= "E.js"></Script><Scripttype= "Text/javascript"src= "F.js"></Script><Scripttype= "Text/javascript"src= "G.js"></Script><Scripttype= "Text/javascript"src= "H.js"></Script><Scripttype= "Text/javascript"src= "I.js"></Script><Scripttype= "Text/javascript"src= "J.js"></Script>
And Requirejs a good solution to the problem, (in the later narrative will talk about the solution)
Second. I believe you should also have encountered JS blocking browser rendering this issue.
<!DOCTYPE HTML><HTML> <Head> <title></title> </Head> <Body> <span>Hello World</span> <Scripttype= "Text/javascript">Alert ('1'); </Script> </Body></HTML>
You will find that when the browser runs, the alert is executed first, when the page should be blank and click OK to load the contents of the body.
This is the result of JS blocking browser rendering.
A classmate will say, can add async and defer what, can also avoid blocking!
In fact, this idea is very good, I think so before. But I did not consider that when the introduction of external JS too many cases, this method is not good to control the order of asynchronous loading, resulting in an error.
And how to control the loading order, then there is a return to the first question. Requirejs will have a good help with all these concerns.
This is what we are going to talk about in the next chapter of the question of dependency.
Here by the way give the students a brief description of the AMD and CMD overview and differences
AMD: is the abbreviation for "Asynchronous module definition", meaning "async module definition". It loads the module asynchronously, and the module's load does not affect the execution of the statement behind it. All statements that rely on this module are defined in a callback function that will not run until the load is complete.
CMD: is the abbreviation for "Common module definition", meaning "common module definition". It uses a delay loading module, which is used when loading the required modules, defined in the unique parameters inside the side package, side loading, edge dependent.
The essential difference between AMD and CMD is that AMD relies on pre-dependencies, and CMD uses deferred dependencies.
Requirejs preliminary understanding of the first, I believe that we should be the same as I have a certain understanding.
The next section describes the dependencies of Requirejs and common APIs. And I hope we can make progress together.
Preliminary mastery of Requirejs