What does the jQuery() function in jQuery do?

2019-03-01 08:27发布

问题:

In this video there is a snippet of code that goes something like this:

if (jQuery) {jQuery(function() {
    // ...
})}

I've never seen the jQuery() function before (then again, I'm not a savvy jQuery user), what does it do? Does it ship by default with jQuery or is it specific to IxEdit? Since the usual $(window).load() snippet is missing and the code is somewhat similar I'm guessing it's a shortcut / alias to:

$(window).load(function() {
    // ...
)}

Am I right? Also what is that jQuery variable? What does it hold? And why is he checking it?

回答1:

$() is an alias for jQuery(), defined as:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

http://code.jquery.com/jquery-1.4.js

there is a special case defined when $() or jQuery() is called with the first argument being a function:

// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
    return rootjQuery.ready( selector );
}

sometimes $ can conflict with other libraries (like prototype) that define the same function, so if you call

jQuery.noConflict();

it will remove the $ alias, setting it back to the original value found, essentially:

window.$ = _$;


回答2:

jQuery(function()

is same as

$(document).ready(function()

if(jQuery)

is a check whether the jQuery.js file has been loaded or not.

There is another way to check this

if (typeof jQuery == 'undefined')
{
    //jQuery has not been loaded  
}


回答3:

The $ function is an alias for the jQuery function. So, they are the same.

If you use jQuery in noConflict mode, there is only jQuery() function



回答4:

I think it is the same that using $() but you use jQuery() for compatibility with other libs which also use $()

jQuery can be a variable that store a function. Guess that if is to check if it is not undefined or something like that