Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of wrapping a jquery function in a closure?

Tags:

jquery

Hi I've been busy trying to take my knowledge of JQuery to the next level, So far I think I've understood everything but as I've ventured onto more advanced tutorials I've noticed several instances where the JQuery routine is wrapped in a closure (see below) however, the thing that confuses me is that it passes a $ and returns JQuery. My question is why? what can I do with the returned JQuery?

I'd really appreciate any light that people can shed on this for me.

(function($){
  $(document).ready(function(){
    var arr = $.map($("LI"), function(item, index){
      while (index < 3)
      {
        return $(item).html();
      }
      return null;
    });
    $(document.body).append("<span>The first three authors are: " +
      arr.join(", ") + "</span>");
  });
})(jQuery);

Thank you in advance.

Rob

like image 595
user432350 Avatar asked Aug 26 '10 21:08

user432350


People also ask

What is a wrapped set in jQuery?

A) Definition Consequently, by definition, a wrapped set is a result of: either a jQuery selector: $('. elementToBeSelected') or a DOM element created on the fly with jQuery: $('<div><span>Test</span></div>'). In this case the wrapped set only contains this single element and its children (if any)

Is jQuery a closure?

jQuery, one of the most popular JS libraries, uses closures to maintain the integrity of its functions and variables. It also uses a self-executing function to further protect its scope.

Why are closures useful?

Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.

How do closures work in JavaScript?

A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environment). In JavaScript, closures are created every time a function is created at run time. In other words, a closure is just a fancy name for a function that remembers the external things used inside it.


1 Answers

It's a self invoking anonymous function (an un-named function that is declared and immediately executed) that takes one argument that gets assigned to the parameter $. The value passed in for the argument is jQuery, the jQuery function.

This is done so that the shorthand $ can be used inside the scope of the function to mean jQuery. Since all of the code inside of the function is in the scope of the function, it's a nice pattern for self containing the code and not polluting the global namespace.

It's also a nice pattern for allowing you to use the $ shorthand for jQuery inside of the function - it could be the case that the $ shorthand (window.$) is assigned something else, as can happen if you use multiple libraries on one page. By using the pattern, you can still use $ to reference jQuery object in the function for familiarity and terseness.

like image 66
Russ Cam Avatar answered Oct 03 '22 11:10

Russ Cam