requirejs如何处理jquery?
我正在阅读一些使用requirejs处理jquery的代码 它使用requirejs如何处理jquery?,jquery,requirejs,Jquery,Requirejs,我正在阅读一些使用requirejs处理jquery的代码 它使用 require([ 'jquery', ], function( $ ){} ); 我想知道为什么所有jquery函数都只使用$就足够了?看看 但是,是的,您只需要$(与jQuery相同),因为jQuery提供的每个方法(或插件)都封装在该变量中 如果您正在使用requirejs加载文件,而您没有这样做,$将是未定义的 网站示例: require(['jquery'], function( $ ) { consol
require([
'jquery',
],
function(
$
){} );
我想知道为什么所有jquery函数都只使用$就足够了?看看
但是,是的,您只需要$
(与jQuery
相同),因为jQuery提供的每个方法(或插件)都封装在该变量中
如果您正在使用requirejs
加载文件,而您没有这样做,$
将是未定义的
网站示例:
require(['jquery'], function( $ ) {
console.log( $ ) // OK
});
require(['jquery'], function( jq ) {
console.log( jq ) // OK
});
require(['jquery'], function( ) {
console.log( $ ) // UNDEFINED!
});
如果
define
函数已经定义,jQuery将自己定义jQuery
模块
以下代码在jquery.js中
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
// Expose jQuery as module.exports in loaders that implement the Node
// module pattern (including browserify). Do not create the global, since
// the user will be storing it themselves locally, and globals are frowned
// upon in the Node module world.
module.exports = jQuery;
} else {
// Otherwise expose jQuery to the global object as usual
window.jQuery = window.$ = jQuery;
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via 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( "jquery", [], function () { return jQuery; } );
}
}
嗯?你还不明白什么?你会期望什么?你可能会误解,
$
有很多属性。我想知道jquery默认支持Requrejs吗?Requirejs需要每个模块使用define。我认为您使用的是require-jquery.js,它已经定义了jquery。如果您正在使用,请考虑不再推荐或维护的内容