First, the basic principle of using SEAJS to develop JavaScript is: Everything is a module. When SEAJS is introduced, the JavaScript code is written into one module after another, and the concept of modules in Seajs is somewhat similar to those in object-oriented classes--modules can have data and methods, data and methods can be defined as public or private, and public data and methods can be called by other modules.
Each module should be defined in a separate JS file, that is, a corresponding module.
Ii. definition and preparation of modules
function (ID, deps, factory) { //Code of Function ...}
If there is only one argument, assign the value to factory.
If there are two parameters, the second is assigned to factory, and the first one is assigned to Deps if it is an array, otherwise it is assigned to the ID.
If there are three parameters, the values are assigned to Id,deps and factory respectively.
However, the official example, including SEAJS, will only pass a factory function in almost all places where define is used, similar to the following code:
Define (function(require, exports, module) { //Code of the module ...});
ID is the identity string of a module, and when define has only one parameter, the ID is assigned by default to the absolute path of this JS file.
There is no special need to recommend not passing in the ID. Deps generally do not need to pass in, the module needs to be loaded with require.
Third, the factory function
Three parameters:
- The require--module load function is used to record dependent modules.
- A exports--interface point that exposes data or methods to external calls when they are defined on them.
- The metadata for the module--module.
Let's talk about module. The module is an object that stores the meta information of the modules, as follows:
- The ID of the module.id--module.
- module.dependencies--an array that stores the list of IDs for all modules that this module relies on.
- module.exports--and exports point to the same object
Four or three modes of writing modules
The first mode to define the module is based on the exports mode:
is a more "authentic" module definition mode. In addition to attaching common data and methods to exports, you can also return an object representing the module directly
Definefunction(Require, exports, module) {varA = require (' a ');//introduction of a module varb = require (' B ');//Introducing the B module varData1 = 1;//Private Data varFunc1 =function() {//Private Methods returnA.run (DATA1); } exports.data2= 2;//Public DataEXPORTS.FUNC2=function() {//Public Methods return' Hello '; }});
The second type: the same as the above mode function
Definefunction(require) {varA = require (' a ');//introduction of a module varb = require (' B ');//Introducing the B module varData1 = 1;//Private Data varFunc1 =function() {//Private Methods returnA.run (DATA1); } return{data2:2, Func2:function() { return' Hello '; } };});
Third: If the module definition has no other code and returns only one object, there is a simplified way to do this: it is appropriate for modules that define pure JSON data.
define ({ 1, function() { return ' Hello '; }});
V. Loading and referencing of modules
As mentioned above, a module corresponding to a JS file, and loading the module is generally provided with a string parameter to tell the loading function needs the module, so there is a set of string identification to the actual module is located in the file path of the parsing algorithm.
Depending on the scenario, SEAJS provides three load module APIs, namely Seajs.use,require and Require.async
1.seajs.use
The seajs.use is mainly used for loading the ingress module.
// Single mode seajs.use ('./a'// callback mode function(a) {// Multi-module mode function (A, b) { a.run (); B.run ();});
SEAJS will parse all dependent modules along the Ingress module and load them. If there is only one entry module, you can omit the seajs.use by adding the "Data-main" attribute to the script tag that introduces Sea.js
<src= "./sea.js" data-main= "./init"></ Script>
2.require
Require is the main module loading method of Seajs, which is usually loaded with require when other modules need to be used in one module:
Automatic loading mechanism for SEAJS. As mentioned above, the use of SEAJS after the HTML as long as the inclusion of sea.js, then the other JS file is how to load it? Seajs will first download the Portal module, and then follow the portal module using regular expressions to match all the require in the code, and then according to the file path in the require to download the corresponding JS file, the downloaded JS file iteration to do similar operations. The entire process is similar to the traversal of the graph (because there may be cross-loop dependencies, so the entire dependent data structure is a graph rather than a tree).
The path identifier passed to require must be a string literal and cannot be an expression, as the following method of using require is wrong:
3.require.async
Seajs will be in the HTML page open with static analysis of all the required JS files, if you want a JS file to download when used, you can use Require.async:
function (m) { //Code of Callback...});
Vi. SEAJS Global Configuration
SEAJS provides a Seajs.config method to set the global configuration and receive a configuration object that represents the global configuration. Here's how to use it:
seajs.config ({ ' path/to/jslib/', alias: { ' app ': ' path/to/app/' }, ' Utf-8 ', 20000, false});
Where base represents the base address path when the base address is addressed
Alias can set abbreviations for longer common paths.
CharSet represents the CharSet property of the script tag when downloading JS.
Timeout indicates the maximum length of time, in milliseconds, to download a file.
Debug indicates whether it is working in debug mode.
SEAJS Study Notes