1

Is this jQuery code

(function(jQuery){
})(jQuery);

equivalent to

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

If yes, what are the differences between the two? If not, what does the first do?

2
  • It is not jQuery code, it is JavaScript code. :) Commented Dec 16, 2010 at 23:57
  • This question is similar to: What is the (function() { } )() construct in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 28, 2024 at 12:12

5 Answers 5

5

They are not equivalent.

Your first example is an Immediately-Invoked Function Expression (IIFE). It creates a closure around locally defined variables.

Your second example specifies a function to execute when the DOM is fully loaded. It is used to ensure that all element nodes in the DOM are available before executing the enclosed code. This is also a closure.

Both examples use anonymous functions.

It is worth pointing out that it is good practice to use both of your examples, like so:

(function($){
    // locally-scoped, DOM-is-NOT-Ready-code here.
    $(function () {
        // your locally-scoped, DOM-is-ready-code here.
    });
}(jQuery)); // note that I've moved the invocation into the parens
            // that contain the function.  This makes JSLint happy!
Sign up to request clarification or add additional context in comments.

2 Comments

Your code example can be simplified into this: jQuery(function($) { });
@Šime Vidas updated my example to make it more clear as to why I would do it this way.
3

Absolutely not, the first one is a self-executing anonymous function and the second is the ready handler.

(function(jQuery){
  //jQuery in this scope is referencing whatever is passed-in
})(jQuery);

So, the jQuery inside of the function isn't necessarily the same jQuery outside the function. But you typically do not want to mix-n-match global variable names with local ones.

Take this example:

(function(obj) {
   alert(obj);
})('Hello');

This defines a function, then immediately invokes it, passing in "Hello"

Comments

3
$(document).ready(function () {

});

is equivalent to this:

$(function() {

});

The first snippet is an immediately invoked anonymous function which creates a local scope:

(function() {
    var x = 2;
})();

alert(x); // undefined

Comments

2

No. The first one isn't really doing much. Just separating any variables inside from the surrounding scope, and creating a local jQuery variable inside.

The second one passes a function that is run after the DOM is ready (in other words after the <body> has loaded).

A common equivalent to:

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

is:

$(function () {
});

which does the same thing.

While this:

(function(jQuery){
})(jQuery);

is often written as:

(function($){
})(jQuery);

so that the $ variable is no longer a global variable. Useful if the global $ variable is already in use.

Comments

1

Not at all. The first one is a closure - a function that you create and then immediately call. However, typically you would combine the two like this:


  (function($) {
    // use the $ variable
    $(document).ready(function(){
      // ...
    });
  })(jQuery);

By creating the closure you are renaming "jQuery" to "$" just locally for that block of code. The reason why you use the closure syntax is so that you can use the $ variable even though it might not be defined as a jQuery object in the global scope (i.e. some JavaScript frameworks like prototype use $ as a variable).

Whenever you write a jQuery plugin you should enclose all jQuery code in this kind of closure so it doesn't interfere with any other JavaScript frameworks. If you aren't writing plugins and you don't use any other JavaScript frameworks you probably don't have to bother enclosing your code in a closure.

4 Comments

Your code can be simplified into this: jQuery(function($) { });
Also, that is not a closure. It's just passing an argument into a function.
@Šime Vidas: I disagree, anytime you have an inner function, you have a closure. If the code where "//..." is creates a handler that uses the $ sign, then he would be using the $ available through the closure. Even if it's not used, it doesn't mean it's not a closure
@Juan Yes, you're right. It does not even have to be a handler inside the ready handler, because the ready handler itself (is an inner function and) survives the outer function's call, which creates the closure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.