skip to Main Content

can anyone help me understand what the following code does?

$(function($) {
    $.supermodal();
});

I see it’s very similar to "wait for document to finish loading before running supermodal". But I don’t understand the 2nd and 3rd dollar signs (I realize they could be any arbitrary variable names).

FWIW, this is from here, where author has an additional usage for changing default values.

Sorry, seems like it should be simple, but it’s one of those things that’s hard to search for when you don’t know what it’s doing (e.g., this isn’t right).

Thanks!

3

Answers


  1. It seems that jquery executes the function it receives as argument. Calling it with first argument as the reference to jquery itself.

    Now the only question is why. From the docs it is to have a failsafe $ alias

    jQuery(function( $ ) {
      // Your code using failsafe $ alias here...
    });
    
    Login or Signup to reply.
  2. The first $ is the global variable to which jQuery is assigned.

    The second $ is a function argument. It defines a variable called $. When you pass a function to jQuery it will be called when the DOM is ready or immediately (whichever is later) and jQuery (specifically the instance of jQuery which was passed the callback function) will be passed to the first argument. This is useful when you are dealing with multiple things which might be assigned to the global $ variable (including multiple versions of jQuery with different plugins … which does happen occasionally).

    The third $ is making use of that argument.

    Login or Signup to reply.
  3. From the documentation:

    Use both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

    jQuery(function( $ ) {
      // Your code using failsafe $ alias here...
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search