0

Can someone please explain in what cases the following syntax can be used and how does this work?

(function($) {
    $.fn.hoverIntent = function(f,g) {
    };
})(jQuery);

Thanks!

Rookie
  • 5,179
  • 13
  • 41
  • 65
  • The question is about the jquery object being passed.. – Rookie Mar 02 '15 at 16:43
  • Ok, I reopened as I chose the wrong duplicate. What you have there is a self-invoking function. – Rory McCrossan Mar 02 '15 at 16:44
  • @RoryMcCrossan I believe the question is about the fact that "jQuery" is passed as an argument, with the formal parameter having the name "$". – Pointy Mar 02 '15 at 16:45
  • yes that's the question – Rookie Mar 02 '15 at 16:45
  • 1
    You should be able to read more about that function [here](https://forum.jquery.com/topic/jquery-ui-modal-form-dialog-has-input-that-does-not-allow-type). Just `ctrl+f` for 'function($)'. It provides some clarity. – TheNorthWes Mar 02 '15 at 16:46
  • 1
    @Rookie it's just insurance - it allows the code inside the anonymous function to refer to the global jQuery object as "$" even if the global "$" is not really jQuery. Of course, the technique doesn't do you any good if the global "jQuery" symbol *also* isn't jQuery. – Pointy Mar 02 '15 at 16:48
  • 1
    Note that the self-invoking function is redundant here anyway, as `jQuery` is provided as a parameter to the document ready handler function by default. – Rory McCrossan Mar 02 '15 at 16:48
  • thanks guys for the comments, i will refer to the links provided! – Rookie Mar 02 '15 at 16:51
  • 1
    The marked duplicate is a terrible choice for this question, because the answer for practical purposes is "Use this pattern to wrap jQuery code that might choke if you use `jQuery.noConflict()`" – jdphenix Mar 02 '15 at 17:15

0 Answers0