前端模块化
前端模块化:前端模块化开发的价值
CMD规范: https://github.com/seajs/seajs/issues/242
sea.js 使用: why sea.js 一步步学会用sea.js
sea.js 官方 http://seajs.org/
github https://github.com/seajs
sea.js的使用利用了模块化,用法跟node.js很像,每个文件都是一个模块,使用define定义:
define(
function
(require,exports,module){}); //require是引用外部文件,exports是暴露个别接口到外部,module暴露整个文件接口。
引用一下官方的实例代码:
// 所有模块都通过 define 来定义 define(function(require, exports, module) { // 通过 require 引入依赖 var $ = require('jquery'); var Spinning = require('./spinning'); // 通过 exports 对外提供接口 exports.doSomething = ... // 或者通过 module.exports 提供整个接口 module.exports = ... });
目前比较流行的规范是CommonJS 和 AMD(异步模块定义规范),后者需要支持浏览器异步特性。
基于后者的规范实现了require.js
我相信,会有一个公正而深刻的认识来为我们总结的:那时,我们这一代独有的奋斗、思索、烙印和选择才会显露其意义。 ——《北方的河》