Home > Article > Web Front-end > About the usage of jQuery compatible with CommonJS and AMD
;(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; });
The above is the detailed content of About the usage of jQuery compatible with CommonJS and AMD. For more information, please follow other related articles on the PHP Chinese website!