jQuery's function($) (jQuery) syntax

11,850

This syntax is not particularly special to jquery, it's normal javascript. Here simply function

function ($) {
    // some code here...
}

(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).

This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.

Share:
11,850
mehdi
Author by

mehdi

Updated on July 20, 2022

Comments

  • mehdi
    mehdi almost 2 years

    Possible Duplicate:
    jQuery: What does (function($) {})(jQuery); mean?

    I stumbled up on the following code (included in one file) but I just cannot understand what it really means.

    (function ($) {
        function doSomething1(somedata) {
    
        }
    
        function doSomething1(somedata) {
    
        }
    })(jQuery);
    

    Question 1: What does this syntax mean in the contex of jQuery

    Question 2: How can we call these functions let say from other files such as the HTML index file and other JavaScript files?

    Thanks