Javascript 使用require.js实现jQuery的正确方法

Javascript 使用require.js实现jQuery的正确方法,javascript,jquery,requirejs,amd,Javascript,Jquery,Requirejs,Amd,我正在使用require.js和jQuery的最新稳定版本,目前我正在使用像这样的jQuery requirejs.config({ paths: { 'jQuery': 'vendor/jquery', } }); require(['jQuery'], function(jQuery) { log(jQuery); // working }); 我不明白的是,我真的不需要显式地返回jQuery,因为这仍然可以工作(在其他模块中也可以): 现在我不确定这是否是正确的方法,还

我正在使用require.js和jQuery的最新稳定版本,目前我正在使用像这样的jQuery

requirejs.config({
paths: {
    'jQuery': 'vendor/jquery',
}
});

require(['jQuery'], function(jQuery) {
    log(jQuery); // working
});
我不明白的是,我真的不需要显式地返回jQuery,因为这仍然可以工作(在其他模块中也可以):


现在我不确定这是否是正确的方法,还因为使用$dollar符号来引用jQuery是行不通的

在我看来,关键点是:

  • 当与RequireJS一起使用时,jQuery将自己注册为命名模块“jQuery”(全部小写)。在您的示例中,您试图将其用作“jQuery”,这有点混淆,因为这也是它在加载时注册的全局函数的名称(请参见第2点)
  • 默认情况下,jQuery使用全局函数“$”和“jQuery”注册自身,即使与AMD/RequireJS一起使用也是如此。如果要关闭此行为,必须调用noConflict函数
  • 您可以在noConflict调用中包装对jQuery的RequireJS引用,如下例所示。据我所知,当您没有其他需要全局$或jQuery的模块时,这是推荐的方法:

    requirejs.config({
        paths: {
            'jquery': 'vendor/jquery',
        }
    });
    
    define('jquery-private', ['jquery'], function (jq) {
        return jq.noConflict( true );
    });
    
    require(['jquery-private'], function(jq) {
        console.log(jq);      // working
        console.log($);       // undefined
        console.log(jQuery);  // undefined
    });
    
  • 另请参阅有关如何映射其他模块以使用专用(noConflict)版本的信息

    有关更多背景信息,请参阅jQuery源代码中的以下几行代码,它们是我上面描述的所有内容的原因:

        // Expose jQuery to the global object
        window.jQuery = window.$ = jQuery;
    
        // Expose jQuery as an AMD module, but only for AMD loaders that
        // understand the issues with loading multiple versions of jQuery
        // in a page that all might call define(). The loader will indicate
        // they have special allowances for multiple jQuery versions by
        // specifying define.amd.jQuery = true. Register as a named module,
        // since jQuery can be concatenated with other files that may use define,
        // but not use a proper concatenation script that understands anonymous
        // AMD modules. A named AMD is safest and most robust way to register.
        // Lowercase jquery is used because AMD module names are derived from
        // file names, and jQuery is normally delivered in a lowercase file name.
        // Do this after creating the global so that if an AMD module wants to call
        // noConflict to hide this version of jQuery, it will work.
        if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
            define( "jquery", [], function () { return jQuery; } );
        }
    

    更新:已更新以反映上述信息。有关包括优化器的逐步步骤,请参见。我只想再次强调,这种无冲突的方法只有在所有插件都兼容AMD的情况下才有效。

    如果您使用的是支持AMD的jQuery的最新版本,您可以点击以下链接:我做了这个,我的大部分脚本都工作了,但在某些情况下,$.fn是未定义的,尽管$看起来还可以。。。知道为什么吗?
        // Expose jQuery to the global object
        window.jQuery = window.$ = jQuery;
    
        // Expose jQuery as an AMD module, but only for AMD loaders that
        // understand the issues with loading multiple versions of jQuery
        // in a page that all might call define(). The loader will indicate
        // they have special allowances for multiple jQuery versions by
        // specifying define.amd.jQuery = true. Register as a named module,
        // since jQuery can be concatenated with other files that may use define,
        // but not use a proper concatenation script that understands anonymous
        // AMD modules. A named AMD is safest and most robust way to register.
        // Lowercase jquery is used because AMD module names are derived from
        // file names, and jQuery is normally delivered in a lowercase file name.
        // Do this after creating the global so that if an AMD module wants to call
        // noConflict to hide this version of jQuery, it will work.
        if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
            define( "jquery", [], function () { return jQuery; } );
        }