jQuery syntax question

Hi, I have the code below which fires when a page has loaded:

(function() {
  (function($) {
      [Some code]            
  })(jQuery);
}).call(this);  

I sort-of get the bit about anonymous functions but I don't understand why the '(jQuery)' is appended at the end of line 4?
Alw1nAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TvMptConnect With a Mentor Commented:
(function($) { })(jQuery); is not tied to any event, it's just a closure so that $ === jQuery inside of it, so you can use the $ shortcut:

(function($) {
  //You may use $ here instead of jQuery and it'll work...even if $ means
  //something else outside of this closure, another library shortcut for example
})(jQuery);
0
 
Jagadishwor DulalBraces MediaCommented:
There is different method to write jquery it's document.ready short code you can write the same code as:
$(document).ready(function(){
      [Some code]            
  })

Open in new window

0
 
leakim971PluritechnicianCommented:
sometime $ is used by an other framework or an other jQuery version.
Passing the jQuery object to the anonymous function allow you to use $ for all your jQuery code inside the anonymous function

[Some code] can safely use $ for a specifc jQuery object an not an other framework or an other jQuery version
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TvMptCommented:
jQuery(function ($) {
});

Open in new window


It will run on document.ready, within a namespace, and with jQuery defined as $, avoiding cross script conflicts
0
 
Alw1nAuthor Commented:
I get that it is to ensure the '$' is tied to jQuery but as per your last post, is:

  (function($) {
      [Some code]            
  })(jQuery);

the same as

  jQuery(function($) {
      [Some code]            
  });
0
 
TvMptCommented:
I put that after the jagadishdulal comment

Alternative shorthand for on document ready for avoiding cross script conflicts
jQuery(function ($) {
});

Open in new window

0
 
Jagadishwor DulalBraces MediaCommented:
No more comments when the points have been assigned.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.