Home > Web Front-end > JS Tutorial > body text

About the usage of jQuery compatible with CommonJS and AMD

一个新手
Release: 2017-09-27 10:07:02
Original
1673 people have browsed it


;(function( global, factory ) {
  //兼容CommonJS
  //这个很重要,npm上面CommonJS规范的模块众多.Node.js是CommonJS规范.vue,angular之类也是babel把es模块转成CommonJS模块.
    if ( typeof module === "object" && typeof module.exports === "object" ) {    //等同于module.exports = jQuery;
        module.exports = global.document ?    //执行效果号下面的function(window,true){}
            factory( global, true ) :      //w等同于global,并且这个函数没有noGlobal参数
            function( w ) {
                if ( !w.document ) {                    
                throw new Error( "jQuery requires a window with a document" );
                }                
                return factory( w );
            };
    } else {
        factory( global );
    }// 没有noGlobal参数,那么jQuery变量就不会被添加到window对象上//小括号里的window或this,函数function 就是上面global和fatory参数})(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
  ......省略代码//AMD规范 require.jsif ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}//没有noGlobal参数,普通浏览器原生JS环境var strundefined = typeof undefinedif ( typeof noGlobal === strundefined ) {
    window.jQuery = window.$ = jQuery;
}return jQuery;

});
Copy after login
;(function( global, factory ) {
  //兼容CommonJS
  //这个很重要,npm上面CommonJS规范的模块众多.Node.js是CommonJS规范.vue,angular之类也是babel把es模块转成CommonJS模块.
    if ( typeof module === "object" && typeof module.exports === "object"){    //等同于module.exports = jQuery;
        module.exports = global.document?    //执行效果号下面的function(window,true){}
            factory( global, true ):      //w等同于global,并且这个函数没有noGlobal参数
            function( w ){
                if ( !w.document ){                    
                throw new Error( "jQuery requires a window with a document");
                }                return factory( w );
            };
    } else {
        factory( global );
    }// 没有noGlobal参数,那么jQuery变量就不会被添加到window对象上//小括号里的window或this,函数function 就是上面global和fatory参数})(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
  ......省略代码//AMD规范 require.jsif ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}//没有noGlobal参数,普通浏览器原生JS环境var strundefined = typeof undefinedif ( typeof noGlobal === strundefined ) {
    window.jQuery = window.$ = jQuery;
}return jQuery;

});
Copy after login

The above is the detailed content of About the usage of jQuery compatible with CommonJS and AMD. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!