Solved

jQuery syntax question

Posted on 2013-06-20
7
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

710 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