1- Environment differentiation

    var global = typeof window! = ="undefined" ? window : this;
    // If global===window: it is running in a browser or webview
    // If running in Node, global may be global or the current module
    var factory = function (window, noGlobal) {
        // Execute this function in the browser environment
           // window -> window
           // noGlobal -> undefined
        // Import execution in webpack environment
          // window -> window
          // noGlobal -> true
          
        "use strict";
        var version = "3.5.1 track of",
            jQuery = function (selector, context) {
                return new jQuery.fn.init(selector, context);
            };
        // ...
        
        if (typeof noGlobal === "undefined") {
            // Browser import directly
            window.jQuery = window.$ = jQuery;
        }
        return jQuery;
    };

    (function (global, factory) {
        "use strict";
        if (typeof module= = ="object" && typeof module.exports === "object") {
            // module & module.exports CommonJS module "Node"
            module.exports = global.document ?
                // Support the CommonJS specification, also have Windows such as: webPack engineering environment
                // =>module.exports=jQuery;
                  //+ import $ from 'jquery' $->jQuery
                  //+ let $=require('jquery') $->jQuery
                factory(global.true) :
                function (w) {
                    if(! w.document) {throw new Error("jQuery requires a window with a document");
                    }
                    return factory(w);
                };
        } else {
            // Do not support the CommonJS specification "browser environment"
            // global->window
            // <script src='jquery.min.js'></script>
            factory(global);
        }
    })(global, factory); 
Copy the code

2- Transfer of authority

function factory(window, noGlobal) {
    "use strict";
    var jQuery = function (selector, context) {
        return new jQuery.fn.init(selector, context);
    };

    // ...

    /* Conflict handling */
     / / the scene
    // 
    // window.$=Zepto;
    // 
    // var _$=window.$; // _$===Zepto
    // jQuery.noConflict = function (deep) {}
    // window.$=jQuery;
    $.noconflict () let jj=$.noconflict (
    
    var _jQuery = window.jQuery,
        _$ = window. $; jQuery.noConflict =function (deep) {
        if (window.$ === jQuery) {
            window. $= _ $; }if (deep && window.jQuery === jQuery) {
            window.jQuery = _jQuery;
        }
        return jQuery;
    };

    /* Expose API */
    if (typeof define === "function" && define.amd) {
        define("jquery"[],function () {
            return jQuery;
        });
    }
    if (typeof noGlobal === "undefined") {
        window.jQuery = window.$ = jQuery;
    }
    return jQuery;
}
Copy the code