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

156
Views
Self Invoking Function as jQuery ducument ready callback

What is the difference between

$(function() {
    // bind some event listeners
});

and

$(function() {
    // bind some event listeners
}());
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

$(function() {
    // bind some event listeners
});

In above case the function is passed to the jquery which will get executed once document is ready.

$(function() {
    // bind some event listeners
}());

In above case the return of the function is passed to the jquery. Since the function is se3lf executing itself, it will get executed immediately and whatever the function returns will get passed to the jquery, so this is not a good way because the objective is to execute the function once document gets ready which is not happening in this case

about 4 years ago · Santiago Trujillo Report

0

$(function(){...}); OR $(document).ready(function(){ ... });

This Function is called when the DOM is ready which means, you can start to query elements for instance. .ready() will use different ways on different browsers to make sure that the DOM really IS ready.

(function(){ ... })();

That is nothing else than a function that invokes itself as soon as possible when the browser is interpreting your JavaScript. Therefore, its very unlikely that you can successfully act on DOM elements here.

about 4 years ago · Santiago Trujillo Report

0

$(function() {
    // bind some event listeners
});

This one will be executed only when the DOM is fully loaded, it's the shortcut for :

$(document).ready(function(){
  // Write code here
}); 

$(function() {
    // bind some event listeners
}());

This one is the same but the function inside the $() is a self invoking function. Usually the goal is to prevent variable name conflicts, because it's creating a new scope.

about 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!