


Introduction to SeaJS in the SeaJS introductory tutorial series (1)_Seajs
Foreword
SeaJS is a JavaScript module loading framework that follows the CommonJS specification and can realize modular development and loading mechanism of JavaScript. Unlike JavaScript frameworks such as jQuery, SeaJS does not extend the encapsulated language features, but only implements JavaScript modularization and module loading. The main purpose of SeaJS is to make JavaScript development modular and easy to load, freeing front-end engineers from the heavy processing of JavaScript files and object dependencies, so they can focus on the logic of the code itself. SeaJS can be perfectly integrated with frameworks such as jQuery. Using SeaJS can improve the readability and clarity of JavaScript code, solve common problems such as dependency confusion and code entanglement in current JavaScript programming, and facilitate code writing and maintenance.
The author of SeaJS is Taobao front-end engineer Yu Bo.
SeaJS itself is developed following the KISS (Keep It Simple, Stupid) concept. It only has a single-digit API, so there is no pressure to learn. In the process of learning SeaJS, you can feel the essence of the KISS principle everywhere - only do one thing and do one thing well.
This article first visually compares traditional JavaScript programming and modular JavaScript programming using SeaJS through an example, then discusses the use of SeaJS in detail, and finally gives some information related to SeaJS.
Traditional mode vs SeaJS modular
Suppose we are developing a web application TinyApp now, and we decide to use jQuery framework in TinyApp. The homepage of TinyApp will use module1.js, module1.js depends on module2.js and module3.js, and module3.js depends on module4.js.
Traditional development
Using traditional development methods, the code of each js file is as follows:
var module1 = {
run: function() {
return $.merge(['module1'], $.merge( module2.run(), module3.run()));
}
}
//module2.js
var module2 = {
run: function() {
. merge(['module3'], module4.run());
}
}
//module4.js
var module4 = {
run: function() {
return ['module4'];
Copy code
The code is as follows:
As the project progresses, there will be more and more js files. Dependencies will also become more and more complex, making the script list in js code and html often difficult to maintain.
SeaJS modular development
Let’s see how to use SeaJS to achieve the same function.
First is index.html:
Copy the code
< ;title>TinyApp