SeaJS is a module loading framework developed by Yubo that follows the CommonJS specification and can be used to easily and happily load any JavaScript module and CSS module style. SeaJS is very small. The small size lies in the fact that the size after compression and gzip is only 4K, and there are very few interfaces and methods. SeaJS has two cores: module definition and module loading and dependencies. SeaJS is very powerful. SeaJS can load any JavaScript module and CSS module style. SeaJS will ensure that when you use a module, other modules it depends on have been loaded into the script running environment. According to Uncle Yu, SeaJS allows you to enjoy the fun of writing code without having to worry about loading problems. Are you tired of so many js and css references? I counted 39 css and js references on the personal homepage of our company website. The impact can be imagined:
1. Not conducive to maintenance, the front end and back end are the same
2. There are too many http requests. Of course, this can be solved by merging, but if there is no direct merging of the back end, the labor cost will be very high. Even if the back end is merged, maintenance , such a long string must be confusing to the eyes
SeaJS can solve these problems very well.
Module definition define
It is relatively simple to define a module, for example, define a sayHello module and create a sayHello.js document:
Module loading use
For example, there is an element with the id "out" on our page and we want to output "Hello SeaJS!",
then we can first introduce sea.js
and then use the sayHello module:
The first parameter is the module representation, which is represented by the relative path relative to sea.js. The ".js" suffix after sayHello.js can be omitted. Of course, there are many ways to identify this module. Please check the official instructions for details. :http://seajs.com/docs/zh-cn/module-identifier.html
The first parameter is a callback function. say.sayHello() is to call the exports.sayHello method of the sayHello module. Of course, there is a say parameter in the callback function.
Module dependencies
Module dependencies should actually exist when the module is defined. For example, let's rewrite the above sayHello module. Suppose we already have a general DOM module, such as some methods of obtaining elements, setting styles, etc. For example, for such a DOM module, write DOM.js as follows
最近这几天一直捣腾SeaJS,越捣腾越喜欢,感谢玉伯!感谢SeaJS!当然你可能觉得这么简单的几个实例没必要这么做。确实如果js文件少的小项目感觉不错模块化的优势,但是,更多的在js文件多或着中型以上项目这个模块化的优势就体现出来了。