Home>Article>Web Front-end> About the usage of jQuery compatible with CommonJS and AMD

About the usage of jQuery compatible with CommonJS and AMD

一个新手
一个新手 Original
2017-09-27 10:07:02 1611browse


;(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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn