( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
//commonJS规范下的环境
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory( global, true ) :/*可以先理解为浏览器环境*/
function( w ) {
/*可以先理解为服务端环境比如node,返回factory函数(调用时需要传入真实的window即带有document属性)
* e.g. var jQuery = require("jquery")(window)
*/
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
// ····
// Expose jQuery and $ identifiers, even in AMD
// (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( typeof noGlobal === "undefined" ) {
window.jQuery = window.$ = jQuery;
// 所以$就可以直接代替jQuery
}
return jQuery;
} );
如果当前环境没有window
对象,将当前上下文环境传入(node环境this等于global,没有document属性);
factory()
函数返回jQuery对象