Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery's function($) (jQuery) syntax [duplicate]

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

like image 778
mehdi Avatar asked Dec 25 '10 18:12

mehdi


1 Answers

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.

like image 197
Nikita Rybak Avatar answered Oct 17 '22 05:10

Nikita Rybak