This article explains the modularization of JavaScript for everyone. The specific content is as follows
AMD is the standardized output of module definition during the promotion process of RequireJS.
Load modules asynchronously, depend on them in advance, and execute them in advance.
Define definition module define(['require','foo'],function(){return});
Require loading module (pre-dependency) require(['foo','bar'],function(foo,bar){});
CMD is the standardized output of module definition during the promotion process of SeaJS.
Define definition exports export define(function(require,exports,module){}); The module stores some objects on the current module.
require(./a) is introduced directly. Require.async is introduced asynchronously.
Synchronous loading, dependencies nearby, delayed execution.
SeaJS application
Official getting started example: http://seajs.org/docs/#quick-start
How to write a SeaJS module?
// 所有模块都通过 define 来定义 define(function(require, exports, module) { // 通过 require 引入依赖 var $ = require('jquery'); var Spinning = require('./spinning'); // 通过 exports 对外提供接口 exports.doSomething = ... // 或者通过 module.exports 提供整个接口 module.exports = ... });
Load module in the page
//在 hello.html 页尾,通过 script 引入 sea.js 后,有一段配置代码: // seajs 的简单配置 seajs.config({ base: "../sea-modules/", alias: { "jquery": "jquery/jquery/1.10.1/jquery.js" } }) // 加载入口模块 seajs.use("../static/hello/src/main")
The above is a brief introduction to JavaScript modularization. I hope it will be helpful for everyone to learn JavaScript modularization.