;(function( global, factory ) {
//兼容CommonJS
//这个很重要,npm上面CommonJS规范的模块众多.Node.js是CommonJS规范.vue,angular之类也是babel把es模块转成CommonJS模块.
if ( typeof module === "object" && typeof module.exports === "object" ) { //等同于module.exports = jQuery;
module.exports = global.document ? //执行效果号下面的function(window,true){}
factory( global, true ) : //w等同于global,并且这个函数没有noGlobal参数
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}// 没有noGlobal参数,那么jQuery变量就不会被添加到window对象上//小括号里的window或this,函数function 就是上面global和fatory参数})(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
......省略代码//AMD规范 require.jsif ( typeof define === "function" && define.amd ) {
define( "jquery", [], function() {
return jQuery;
});
}//没有noGlobal参数,普通浏览器原生JS环境var strundefined = typeof undefinedif ( typeof noGlobal === strundefined ) {
window.jQuery = window.$ = jQuery;
}return jQuery;
});;(function( global, factory ) {
//兼容CommonJS
//这个很重要,npm上面CommonJS规范的模块众多.Node.js是CommonJS规范.vue,angular之类也是babel把es模块转成CommonJS模块.
if ( typeof module === "object" && typeof module.exports === "object"){ //等同于module.exports = jQuery;
module.exports = global.document? //执行效果号下面的function(window,true){}
factory( global, true ): //w等同于global,并且这个函数没有noGlobal参数
function( w ){
if ( !w.document ){
throw new Error( "jQuery requires a window with a document");
} return factory( w );
};
} else {
factory( global );
}// 没有noGlobal参数,那么jQuery变量就不会被添加到window对象上//小括号里的window或this,函数function 就是上面global和fatory参数})(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
......省略代码//AMD规范 require.jsif ( typeof define === "function" && define.amd ) {
define( "jquery", [], function() {
return jQuery;
});
}//没有noGlobal参数,普通浏览器原生JS环境var strundefined = typeof undefinedif ( typeof noGlobal === strundefined ) {
window.jQuery = window.$ = jQuery;
}return jQuery;
});以上就是关于jQuery 兼容CommonJS,AMD的用法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号