$(document).ready(function(){});
$(function(){});
jQuery(document).ready(function($){});
Run Code Online (Sandbox Code Playgroud)
我不确定我是否完全理解#2中发生的事情,以及为什么它等同于标准的方式,#1.
jQuery而$实际上是相同的.
如果将函数传递给函数$(),jQuery基本上会检查它的类型,如果它是一个函数,它将在DOM准备好时执行它.这只是Javascript:
function myFunc(arg){
if(typeof arg == 'function'){
arg.call();
}
}
Run Code Online (Sandbox Code Playgroud)
来自jQuery源码:
// First, jQuery saves the old values of window.jQuery and window.$
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$,
...
// Later on, jQuery returns a reference to the actual jQuery object:
window.jQuery = window.$ = jQuery
...
// and if you use noConflict, it'll replace window.$ (and window.jQuery) with the old values again
noConflict: function( deep ) {
window.$ = _$;
if ( deep ) {
window.jQuery = _jQuery;
}
return jQuery;
}
Run Code Online (Sandbox Code Playgroud)
如果你调用jQuery函数,它将检查参数类型:
init: function( selector, context ) {
...
else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
Run Code Online (Sandbox Code Playgroud)
在哪里rootjQuery相同jQuery(document)