First, Seajs1. General steps for using SEAJS
A) introducing sea.js on the main page
b) Write module
c) Use the module on the main page
2. The notation of the module
Math.js
1Definefunction(Require, exports, module) {2 varSquareobj=require ("Square");3module.exports={4Addfunction(A, b) {5 returnA +b;6 },7Cubefunction(a) {8 returnSquareobj.square (a) *A;9 }Ten } One})
Square.js
Define (function() { return { square:function(a) { return A * a ;}} })
Define is a fixed usage that defines a module, where the parameters are represented by:
Require: Dependent files, the parameters inside are relative to the path of the sea.js file
Exports: You can export a method, but it cannot export an object
Module.exports: You can export a method or export an object
If you need to export a number of methods to use Module.exports, only one is used exports.
These parameters are not required, but it is important to note that if you want to use exports, the front must have require, because the position of the parameter cannot be changed.
3. Using the module
<!DOCTYPE HTML><HTML><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <Scriptsrc= "Sea-debug.js"></Script> <Script>Seajs.config ({base:"Module", alias:{query:"jquery-1.12.4"}}) Seajs.use ("Math.js",function(obj) {console.log (Obj.add (8,3)); Console.log (Obj.cube (8,3)); }) </Script></Head><Body></Body></HTML>
Seajs.config: Config seajs,base is set directory, alias is set alias
Seajs.use: Using modules
Parameter one: The path to the module used, because base sets the base path, so the path here is the path on the base path; if more than one module is used, the array is wrapped in multiple modules
Parameter two: function, the parameter inside the function represents the object returned by each module, there are multiple modules there are multiple objects, in this function can use the method in the module
Second, Requirejs
The usage of Requirejs and SEAJS is basically the same, first introduces, writes the module, finally uses the module, the difference is uses the module.
Requirejs Modules for use:
<!DOCTYPE HTML><HTML><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <Scriptsrc= "Require.js"></Script> <Script>Requirejs.config ({baseUrl:"Scripts/module", }) Requirejs (["Math.js"],function(obj) {console.log (Obj.add (8,3)); Console.log (Obj.cube (8,3)); }) </Script></Head><Body></Body></HTML>
Iii. the difference between Seajs and Requirejs
1. Requirejs can set the template loading option in the script tag that introduces Require.js so that the first module can be loaded
<data-main= "Script/main" src= "Require.js"></ script>
2, the Requirejs configuration set the base path of the property is BaseURL, its path is relative to the path of the page
3, Requirejs do not need to write when using the module. Use, its first parameter must be a number of arrays, even if it only introduces a module, but also in the form of an array
4, Seajs is lazy loading, lazy loading is when to call, when loading, Requirejs is preloaded, loaded, and then executed, so the order on some output will be different
5, Seajs is based on the CMD specification, Requirejs is based on the AMD specification
Iv. Seajs use of jquery
Since Seajs is based on the CMD specification, and jquery is based on the AMD specification, SEAJS will need to change the jquery specification to cmd when using jquery.
Here I modified the jquery-1.12.4.js, modified the position in 10972 lines, the AMD changed to CMD, the comment part of the original code, the following code is modified
if typeof Define = = = "function" && define. cmd ) { //define ("jquery", [], function () { // return jquery; //} ); Define (function(require,exports,module) { module.exports=jQuery; });}
V. Other small points of knowledge
1, in the writing path, "Main.js" can be written as "main", because in Seajs and Requirejs inside will add to us. js
2. If the path is at the same level as the file directory that you are writing, you can use "./main" to load the path./is to find in this file directory
3, the module, you can use the exports export method, with Module.exports to export the object or method, you can also use return to export the object or method
4, Seajs and Requirejs principle is to create a script tag, src equals the file path to be introduced, appended to the head, after the reference is removed
Seajs and Requirejs