SeaJS 是一個適用於 Web 瀏覽器端的模塊加載器。使用 SeaJS,能夠更好地組織 JavaScript 代碼。node
在 SeaJS 的世界裏,一個文件就是一個模塊。全部模塊都遵循 CMD 規範,能夠像在 Node 環境中同樣來書寫模塊代碼:jquery
define(function(require, exports, module) { var $ = require('jquery'); exports.sayHello = function() { $('#hello').toggle('slow'); }; });
將上面的代碼保存爲 hello.js
,而後就能夠經過 SeaJS 來加載使用了:git
seajs.config({ alias: { 'jquery': 'http://modules.seajs.org/jquery/1.7.2/jquery.js' } }); seajs.use(['./hello', 'jquery'], function(hello, $) { $('#box').click(hello.sayHello); });
SeaJS的優點:github
SeaJS 帶來的最大好處是:提高代碼的可維護性。若是一個網站的 JS 文件超過 3 個,就適合用 SeaJS 來組織和維護代碼。涉及的 JS 文件越多,SeaJS 就越適合。瀏覽器
學習SeaJS 能夠讀讀下面的文檔:異步
遇到問題時,能夠到 SeaJS 社區 尋找幫助。學習
SeaJS 兼容全部 PC 和 Mobile 端的主流瀏覽器:網站
IE 5.5+ ✔ Chrome 3+ ✔ Firefox 2+ ✔ Safari 3.2+ ✔ Opera 10+ ✔