Solved

jQuery syntax question

Posted on 2013-06-20
7
244 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add or delete table rows 10 55
Binding a Grid in Javascript 5 46
Asp in server side with Mssql Server 7 4 29
JQuery Autocomplete Tag AJAX (Need nice script) 11 37
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

821 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