Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean whenever a function ends with })(jQuery);?

Tags:

jquery

I tried googleing, but Google doesn't seem to care about parentheses...

like image 464
Himmators Avatar asked Nov 12 '10 17:11

Himmators


People also ask

What does function ($) jQuery mean?

(function($) { // do something })(jQuery); this means, that the interpreter will invoke the function immediately, and will pass jQuery as a parameter, which will be used inside the function as $ .

How call jQuery function in if condition?

var flagu6=0; if( flagu1==0 && flagu2==0 && flagu3==0 && flagu4==0 && flagu6==0 ) return true; else return false; } function clearBox(type) { // Your implementation here } // Event handler $submitButton. on('click', handleSubmit); }); Now, clicking the button will go to handleSubmit function, which employs your code.


2 Answers

If you see this:

(function($) {
    // ...code using $...
})(jQuery);

It's doing two things:

  1. Defining an anonymous function that uses $ as its reference to jQuery.
  2. Calling it, passing in jQuery.

You could do it like this:

function foo($) {
    // ...code using $...
}
foo(jQuery);

...but that creates an unnecessary symbol.

All of this is because jQuery has the symbol jQuery and the symbol $, but it's not uncommon for people to use jQuery.noConflict() to tell jQuery to return $ back to whatever it was when jQuery loaded, because a couple of other popular libraries (Prototype and MooTools, to name two) use $ and this lets someone use those libraries and jQuery together. But you can still use $ within your function, because the argument shadows whatever that symbol means outside the function.

like image 176
T.J. Crowder Avatar answered Oct 12 '22 08:10

T.J. Crowder


It basically automatically invokes the anonymous/lambda function defined and supplies the jQuery reference to it.

Pretty much the same as functionCall(jQuery) except you define it and invoke it in the same line/expression.

like image 22
meder omuraliev Avatar answered Oct 12 '22 08:10

meder omuraliev