function fun1() {
// some code here
}
function fun2() {
// some other code here
}
...
var com = com || {};
com.zfanw = com.zfanw || {};
com.zfanw.module1 = (function() {
// some code here
return {
func1: func1,
...
};
}());
com.zfanw.module2 = (function() {
// some other code here
return {
func1: func1,
...
};
}());
...
com.zfanw.module1.func1.apply({},['arg1',arg2]);
...
(function($, mod1) {
// some code here
mod1.func1.apply({},['arg1',arg2]);
}(jQuery, com.zfanw.module1));
...
define(id?, dependencies?, factory); // ? 表示可选项
define(function() {
// some code here
return {
// some public api
};
});
define(['jquery'], function($) { // 比如这个模块,代码的执行依赖 jQuery,require.js 会先加载 jquery 模块代码,并加以执行,然后将依赖模块 以 $ 的参数形式传入回调函数中,回调函数将执行结果注册为模块
// maybe some code here
return {
// some public api
};
});
(function($, mod1) {
// some code here
mod1.func1.apply({},['arg1',arg2]);
}(jQuery, com.zfanw.module1));
define('libs/hi', ['jquery'], function($){......});
require(['jquery'], function($) { // 这个函数加载 jquery 依赖,然后执行回调代码
console.log($);
});
index.html js/ main.js require.js jquery.js
<script src="js/require.js" data-main="js/main"></script>
require(['jquery'], function($) {
var w = $(window).width();
console.log(w);
});
require.config({
shim: {
'jquery-1.4.1': { // <= 这个是相对于 main.js 的路径www.45it.com
exports: 'jQuery' // <= 这个值需要稍加注意,稍后会提及
},
'libs/jquery-throttle-debounce.min': { // <= jQuery 插件
deps: ['jquery-1.4.1'] //无需 exports,因为我们只是在增强 jQuery 功能
}
},
});
require(['jquery-1.4.1', 'libs/jquery-throttle-debounce.min'], function($){
console.log($.debounce);
});
require.config({
paths: {
'jquery': 'jquery-1.4.1', // <= 模块 jquery 指向 js/jquery-1.4.1.js 文件
'debounce': 'libs/jquery-throttle-debounce.min'
},
shim: {
'jquery': {
exports: '$'
},
'debounce': {
deps: ['jquery']
}
}
});
require(['jquery', 'debounce'], function($){
console.log($.debounce);
});
require.config({
paths: {
'jquery': 'jquery-1.4.1',
},
shim: {
'jquery': {
exports: 'hellojQuery' // 这里我把 exports 值设置为 jQuery/$ 以外的值
}
}
});
require(['jquery'], function($){
console.log($);// 这里,会显示 undefined
});
<script src="js/a.js"></script> <script src="js/b.js"></script> <script src="js/c.js"></script>
require(['jquery'], function(){
console.log(jQuery);
console.log($);
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有