Step by step you analyze Requirejs

Source: Internet
Author: User

Detailed source code altogether more than 2000 lines, to see me this analysis classmate should all download down, OK, words not to say, start:

3 global variables appear at the beginning of the code: REQUIREJS, Require, define

var Requirejs, require, define; (function (Global, setTimeout) {Balababla ...}) (This, (typeof setTimeout = = = ' undefined '? undefined:settimeout)))

  

Require and define everyone should know what to do, to tell the truth, I do not know, in the analysis of the code, I have never used this framework, have heard AMD, just to see the source directly.

If you are not very clear, these 2 variables is what to do, I would like to introduce briefly, understand the classmate if I said wrong, I hope to guide me also a bit.

Main Page index.html:

Note that SRC is the introduction of our Requirejs Library, Data-main: That's where we first used Requrie:

Main.js:

Here are 2 blocks of code that are dependent on require:

(1) Require.config: Configuration

(2) Requrie (); Load the required functions, note that the [' name ', ' say '] are all filenames, and they are all in the./js/directory, specifically see CONIFG

Require.config ({  baseUrl: ',  paths: {    ' namedep ': ' JS/NAMEDEP ',    ' say ': ' Js/say ',    ' name ': ' js/ Name '  },  shim: {    ' name ': {      deps: [' NAMEDEP '}}}},  require ([' name ', ' say '], function ( Name, say) {  say (name);});

  

./js/name.js and./js/say.js

Namedefine (['], function () {  return ' test ';});

  

Saydefine ([], function () {  return function (name) {    console.log (name);  };});

  

Finally note that there is a skim in config, which is also the definition of JS file, just because he may not conform to the AMD-loaded specifications

./js/namedep.js

Console.log ("Namedep.js")

  

--------------------------------------Split Line-------------------------------------------------------------------------

Step by step you analyze Requirejs

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.