SeaJS is a JavaScript module loading framework that follows the CommonJS specification. It is a modern module loading tool for web development, providing a simple and ultimate modular experience. Sea.js is jointly maintained by Alibaba, Tencent and other companies.
Benefits of using Sea.js:
Sea.js pursues a simple and natural way of writing and organizing code, and has the following core features:
Simple and friendly module definition specification: Sea.js follows the CMD specification and can write module code like Node.js.
Natural and intuitive code organization: automatic loading of dependencies and concise and clear configuration allow us to enjoy coding more.
Sea.js also provides commonly used plug-ins, which are very helpful for development, debugging and performance optimization, and have rich extensible interfaces.
The following is an introduction to the three modes of writing modules in sea.js
Use exports. Exports is an object used to provide module interfaces to the outside world.
define(function (require, exports, module) { var a = require("./init"); var fun1 = function () { return a.write("模块main调用模块init的write方法"); }; exports.fun1=fun1; });
In addition to adding members to the exports object, you can also use return to directly provide interfaces to the outside world.
define(function(require,exports,module){ var a = require("./init"); var fun1 = function () { return a.write("模块main调用模块init的write方法"); }; return{ fun1:fun1 } })
If the module does not have any business logic and just returns an object, it can be simplified to the following
define({ fun1 : function () { alert("模块main的fun1调用成功") } });
Another method is to provide a unified interface to the outside world through module.exports. For example:
define(function(require,exports,module){ var a = require("./init");// ./是当前目录 ../是上级目录 /是根目录 var fun1 = function () { return a.write("模块main调用模块init的write方法"); }; exports.b=function(){ //没有任何意义,赋值无效 alert("bb") }; module.exports={ fun1:fun1 } });
exports is just a reference to module.exports. When exports is reassigned inside the method, the value of module.exports will not be changed. Therefore, assigning a value to exports is invalid. The above method only exposes one fun1 to the outside. The above b method assignment is invalid and cannot be used to change the module interface.
exports.async()
require.async(id||[], callback?)
The require.async method is used to load the module asynchronously inside the module and execute the specified callback after the loading is completed. The callback parameter is optional.
define(function(require,exports,module){ require.async('./init',function(a){ a.write("模块main调用模块init的write方法") }); require.async(['./init',"./search"],function(a,b){ a.write("模块main调用模块init的write方法"); b.search("search模块成功引入") }); });
Module module is an object that stores some properties and methods associated with the current module.
1 module.id String
The unique identifier of the module.
2 module.uri String
The absolute path of the module obtained according to the path parsing rules of the module system. Under normal circumstances (when the id parameter is not handwritten in define), the value of module.id is module.uri, and they are exactly the same.
3 module.dependencies Array
dependencies is an array representing the dependencies of the current module.