1. // 使用立即执行函数,包装一层,解决不同环境的加载问题,比如浏览器端或者node.js中的加载
    2. ( function( global, factory ) { //第一个参数为全局变量,第二未构造工厂。通过判断全局变量来确定环境。
    3. "use strict";
    4. // 判断是否为CommonJS规范的环境(node.js是这个规范),或者类似这个规范
    5. // 这个规范会使用同步式的module.exports来导出模块。
    6. if ( typeof module === "object" && typeof module.exports === "object" ) {
    7. // For CommonJS and CommonJS-like environments where a proper `window`
    8. // is present, execute the factory and get jQuery.
    9. // For environments that do not have a `window` with a `document`
    10. // (such as Node.js), expose a factory as module.exports.
    11. // This accentuates the need for the creation of a real `window`.
    12. // e.g. var jQuery = require("jquery")(window);
    13. // See ticket #14549 for more info.
    14. // 此处注意 global传入时 全局存在 window对象时,global为window,否则为整个函数外部的的this
    15. module.exports = global.document ?
    16. // 如果存在document属性则执行构建工厂,并传入global对象,并标记为非全局。
    17. factory( global, true ) :
    18. function( w ) { // 调用说明node.js需要使用 var jQuery = require("jquery")(window);
    19. if ( !w.document ) { // 校验后续使用构建工厂时,是否传入了正确的对象,(多半为window)
    20. throw new Error( "jQuery requires a window with a document" );
    21. }
    22. return factory( w );
    23. };
    24. } else {
    25. // 非 commonjs环境,直接执行构建工厂,并标记为全局
    26. factory( global );
    27. }
    28. } )( typeof window !== "undefined" ? window : this, // node.js下不存在window,this为global对象。node下 this === global
    29. function( window, noGlobal ) {
    30. var
    31. version = "3.5.1",
    32. // Define a local copy of jQuery
    33. jQuery = function( selector, context ) {
    34. // The jQuery object is actually just the init constructor 'enhanced'
    35. // Need init if jQuery is called (just allow error to be thrown if not included)
    36. return new jQuery.fn.init( selector, context );
    37. };
    38. // 其余的代码,补充jQuery功能,此处省略
    39. // Edge <= 12 - 13+, Firefox <=18 - 45+, IE 10 - 11, Safari 5.1 - 9+, iOS 6 - 9.1
    40. // throw exceptions when non-strict code (e.g., ASP.NET 4.5) accesses strict mode
    41. // arguments.callee.caller (trac-13335). But as of jQuery 3.0 (2016), strict mode should be common
    42. // enough that all such attempts are guarded in a try block.
    43. "use strict";
    44. // Register as a named AMD module, since jQuery can be concatenated with other
    45. // files that may use define, but not via a proper concatenation script that
    46. // understands anonymous AMD modules. A named AMD is safest and most robust
    47. // way to register. Lowercase jquery is used because AMD module names are
    48. // derived from file names, and jQuery is normally delivered in a lowercase
    49. // file name. Do this after creating the global so that if an AMD module wants
    50. // to call noConflict to hide this version of jQuery, it will work.
    51. // Note that for maximum portability, libraries that are not jQuery should
    52. // declare themselves as anonymous modules, and avoid setting a global if an
    53. // AMD loader is present. jQuery is a special case. For more information, see
    54. // https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon
    55. // 检测是否为 AMD 规范
    56. if ( typeof define === "function" && define.amd ) {
    57. define( "jquery", [], function() {
    58. return jQuery;
    59. } );
    60. }
    61. // 执行 window.jQuery = window.$ = jQuery;之前先保存 原始的 window.jQuery和window.$;
    62. var
    63. // Map over jQuery in case of overwrite
    64. _jQuery = window.jQuery,
    65. // Map over the $ in case of overwrite
    66. _$ = window.$;
    67. jQuery.noConflict = function( deep ) {
    68. if ( window.$ === jQuery ) {
    69. // 如果 window.$是被jQuery自身占用,则退回原始的值。避免因为不检测而直接复原
    70. window.$ = _$;
    71. }
    72. // 同理,传入deep为true,则将window.jQuery也进行检测还原
    73. if ( deep && window.jQuery === jQuery ) {
    74. window.jQuery = _jQuery;
    75. }
    76. // 同时返回jQuery对象,不然后续无法进行调用
    77. return jQuery;
    78. };
    79. // Expose jQuery and $ identifiers, even in AMD
    80. // (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
    81. // and CommonJS for browser emulators (#13566)
    82. // 全局则进行挂载对象到window
    83. if ( typeof noGlobal === "undefined" ) {
    84. window.jQuery = window.$ = jQuery;
    85. }
    86. // 返回jQuery对象,给common.js模块使用。
    87. // 不然无需返回值。
    88. return jQuery;
    89. } );