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 simple and natural code writing and organization methods, with 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: the automatic loading of dependencies and the concise and clear configuration allow us to enjoy more coding.
Sea.js also provides common plugins, which are very helpful for development debugging and performance optimization, and have rich extensible interfaces.
Below I will introduce the three modules of sea.js
Use exports, exports is an object that provides module interfaces to the outside.
define(function (require, exports, module) {var a = require("./init");var fun1 = function () {return a.write("Module main calls the write method of module init");};exports.fun1=fun1;});In addition to adding members to the exports object, you can also use return to provide interfaces directly to the outside.
define(function(require,exports,module){var a = require("./init");var fun1 = function () {return a.write("Module main calls the write method of module init");};return{fun1:fun1}})If the module does not have any business logic, just return an object, it can be simplified to the following
define({fun1 : function () {alert("Fun1 call of module main successfully")}});Another way is to provide a unified interface through module.exports, for example:
define(function(require,exports,module){var a = require("./init");// is the current directory../ is the upper directory/is the root directory var fun1 = function () {return a.write("module main calls the write method of module init");};exports.b=function(){ //No meaning, the assignment is invalid, alert("bb")};module.exports={fun1:fun1}});exports is just a reference to module.exports. When reassigning exports inside the method, the value of module.exports is not changed. Therefore, assigning the exports value is invalid. The above method only exposes an external fun1. The above method B 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("module main calls the write method of module init")});require.async(['./init',"./search"],function(a,b){a.write("module main calls the write method of module init");b.search("search module successfully introduced")});});module module is an object that stores some properties and methods associated with the current module.
1 module.id String
Unique identification of the module.
2 module.uri String
The absolute module path obtained according to the path analysis rules of the module system. Generally, the value of module.id is module.uri, which is exactly the same.
3 module.dependencies Array
dependencies is an array representing the dependencies of the current module.