Solved

jQuery syntax question

Posted on 2013-06-20
7
241 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

705 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

15 Experts available now in Live!

Get 1:1 Help Now