频道栏目
首页 > 资讯 > JavaScript > 正文

jquery的加载运行环境代码分析

18-06-02        来源:[db:作者]  
收藏   我要投稿

jquery的加载运行环境

开发环境下jquery3.3.1的代码结构如下所示:

( function( global, factory ) {
    "use strict";
    if ( typeof module === "object" && typeof module.exports === "object" ) {
        console.log(module)
        console.log(module.exports)
        console.log(global)
        console.log(global.document)
        // 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 ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };

        console.log(module.exports)
    } else {
        console.log(global)
        factory( global );
    }
// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) { ... });

nodejs环境下(CommonJS )

module是一个对象,window是undefined,module.exports会返回一个函数,当调用$.isFunction等类似的静态方法时都会输出undefined

浏览器环境(CommonJS-like )

这里我是用webpack+vue做的测试demo,webpack来做模块的管理,在vue的单文件组件里import jquery脚本,会通过factory( global, true )返回一个jquery对象

浏览器环境(在html文件里引入)

在html文件里通过外部脚本的方式引入jquery,会通过执行factory( global )将jquery对象挂载到window对象上

相关TAG标签
上一篇:JavaScript中变量、常量与字面量的快速理解
下一篇:解决ListViewItemCheckBox重复选择
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站