Maison  >  Article  >  interface Web  >  À propos de l'utilisation de jQuery compatible avec CommonJS et AMD

À propos de l'utilisation de jQuery compatible avec CommonJS et AMD

一个新手
一个新手original
2017-09-27 10:07:021634parcourir


;(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;

});
;(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;

});

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn