Solved

jQuery syntax question

Posted on 2013-06-20
7
242 Views
Last Modified: 2013-06-20
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?
0
Comment
Question by:Alw1n
7 Comments
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
ID: 39262545
(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
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39262549
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39262553
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:TvMpt
ID: 39262561
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
 

Author Comment

by:Alw1n
ID: 39262592
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
 
LVL 9

Expert Comment

by:TvMpt
ID: 39262633
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
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39262658
No more comments when the points have been assigned.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 55
Sums of coloumns in html/java 15 66
In Angular, how can I programatically fire a function 8 54
Split in Javascript 5 31
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now