依据require实现的简易的前端模块化框架。若是你不想花时间学习require.js,也不想翻看长篇的cmd/amd规范,那么这个mini-define就是你不错的选择。若是你以前用过sea.js或require.js那么mini-define更加高效,更加轻量,更加易用。javascript
项目地址:html
define.js: https://github.com/bjtqti/study/tree/master/define前端
require.js https://github.com/bjtqti/study/tree/master/requirejava
首先定义模块git
定义模块 github
1.1 根据是否有依赖,有两种状况: 1.1.1:没有依赖的模块
define('id',function(){ // put your code here });
1.1.2:有依赖的模块
define('id',['modeA','modeB'],function(A,B){ // put your code here });
1.2 根据是否须要返回处理结果给外部使用,又能够分两种状况: 1.2.1有返回对象:
define('id',function(){ return { // put your code here } });
1.2.2 没有返回对象
define('id',function(){ // put your code here });
2.1 根据请求的模块数,能够有两状况: 2.1.1.调用单个模块 require('modeId') 2.1.2.调用多个模块
require(['modeA','modeB']);
2.2 根据是否有回调处理,又能够分为两种状况: 2.2.1 有回调处理函数
require('modeId',function(mode){ //put your code here }); require(['modeA','modeB'],function(A,B){ //put your code here });
2.2.2 没有回调处理
require('modeId');
而后在index.html页面依次引用所需模块框架
<!--核心模块-->
<script src="lib/core/require.js"></script> <!--用于演示的模块--> <script src="lib/main.js"></script> <script src="lib/config.js"></script> <script src="lib/init.js"></script>
最后就是用你喜欢的方式对lib目录进行合并压缩,生成一个min.js文件。 在发布应用的时候,相应的index.html也须要调整一下:模块化
<script src="lib/min.js"></script>