Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

390
Views
jQuery dollar sign ($) as function argument?

I understand JavaScript closures, and I've seen this done in native JS:

(function () {
  // all JS code here
})();

But, what does adding the jQuery spice do?

(function ($) {
  // all JS code here
})(jQuery);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

When you see:

(function() {
  // all JS code here
})();

It is knows as self-invoking anonymous function. The function executes as soon as it is parsed because of the addition of () at the end (that's how you run js functions).

Notice that extra outer braces are just convention, you could also write it up like:

function() {
  // all JS code here
}();

But that convention is heavily used all around and you should stick to it.

(function($) {
  // all JS code here
})(jQuery);

Here, $ is mapped to jQuery object so that you could use $ instead of jQuery keyword. You could also put some other character there too:

(function(j) {
  // all JS code here
})(jQuery);

Here, j is mapped to jQuery object instead.

Notice also that arguments specified to self-invoking function remain within the scope of that function and do not conflict with outer scope/variables.


I had written an article on the subject, please check it out:

  • Javascript Self-executing Functions
over 4 years ago · Santiago Trujillo Report

0

Its a way of mapping jQuery to the $ in a way so that not all code in a page will see it.

Maybe you have an existing script that uses jQuery that you like to reuse but you also use prototype that also uses $ in the same page.

By wrapping any jQuery using code in that construct you redefine $ to jQuery for the contained part without coming into conflict with other code in the page.

over 4 years ago · Santiago Trujillo Report

0

(function() {
   // all JS code here
})();

Is an Immediately-Invoked Function Expression (IIFE), pronounced "iffy". Some people also call them "anonymous, self-executing functions", or just "self-executing functions".

(function(aParameter) {
   alert(aParameter);  // will alert "an argument"
})("an argument");

Here's an IIFE which takes the parameter 'aParameter' and passes itself the argument "an argument".

(function($){
   alert($(window).jquery);  // alert the version of jQuery
})(jQuery);

This one is similar, but "jQuery" (THE jQuery object instance) is the argument to the IIFE, and in this case, jQuery gets passed as the '$' parameter. In this way, simply by typing '$', the body of the IIFE has access to jQuery and its members. This is a common jQuery convention, and it's common for people writing jQuery plugins to maintain this convention in this way.

Not only does the above code maintain the jQuery convention, it also ensures that neither you nor anyone else can accidentally break that convention. E.g., take the following code:

var $ = function() {
   alert('foo');
}

This code turns '$' into something which is definitely not jQuery. If someone did this in some other code outside your plugin code, and then you didn't explicitly pass jQuery as '$' to your plugin, then you wouldn't be able to use '$' as jQuery like you usually do.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!