// 使用立即执行函数,包装一层,解决不同环境的加载问题,比如浏览器端或者node.js中的加载
( function( global, factory ) { //第一个参数为全局变量,第二未构造工厂。通过判断全局变量来确定环境。
"use strict";
// 判断是否为CommonJS规范的环境(node.js是这个规范),或者类似这个规范
// 这个规范会使用同步式的module.exports来导出模块。
if ( typeof module === "object" && typeof module.exports === "object" ) {
// 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.
// 此处注意 global传入时 全局存在 window对象时,global为window,否则为整个函数外部的的this
module.exports = global.document ?
// 如果存在document属性则执行构建工厂,并传入global对象,并标记为非全局。
factory( global, true ) :
function( w ) { // 调用说明node.js需要使用 var jQuery = require("jquery")(window);
if ( !w.document ) { // 校验后续使用构建工厂时,是否传入了正确的对象,(多半为window)
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
// 非 commonjs环境,直接执行构建工厂,并标记为全局
factory( global );
}
} )( typeof window !== "undefined" ? window : this, // node.js下不存在window,this为global对象。node下 this === global
function( window, noGlobal ) {
var
version = "3.5.1",
// Define a local copy of jQuery
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.fn.init( selector, context );
};
// 其余的代码,补充jQuery功能,此处省略
// Edge <= 12 - 13+, Firefox <=18 - 45+, IE 10 - 11, Safari 5.1 - 9+, iOS 6 - 9.1
// throw exceptions when non-strict code (e.g., ASP.NET 4.5) accesses strict mode
// arguments.callee.caller (trac-13335). But as of jQuery 3.0 (2016), strict mode should be common
// enough that all such attempts are guarded in a try block.
"use strict";
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
// Note that for maximum portability, libraries that are not jQuery should
// declare themselves as anonymous modules, and avoid setting a global if an
// AMD loader is present. jQuery is a special case. For more information, see
// https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon
// 检测是否为 AMD 规范
if ( typeof define === "function" && define.amd ) {
define( "jquery", [], function() {
return jQuery;
} );
}
// 执行 window.jQuery = window.$ = jQuery;之前先保存 原始的 window.jQuery和window.$;
var
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$;
jQuery.noConflict = function( deep ) {
if ( window.$ === jQuery ) {
// 如果 window.$是被jQuery自身占用,则退回原始的值。避免因为不检测而直接复原
window.$ = _$;
}
// 同理,传入deep为true,则将window.jQuery也进行检测还原
if ( deep && window.jQuery === jQuery ) {
window.jQuery = _jQuery;
}
// 同时返回jQuery对象,不然后续无法进行调用
return jQuery;
};
// Expose jQuery and $ identifiers, even in AMD
// (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
// 全局则进行挂载对象到window
if ( typeof noGlobal === "undefined" ) {
window.jQuery = window.$ = jQuery;
}
// 返回jQuery对象,给common.js模块使用。
// 不然无需返回值。
return jQuery;
} );