1

I fount that code

(function($) {
    Batch = {
        ...
        ...
})(jQuery);

Why pass a reference of JQuery as a parameter?

afdi5
  • 307
  • 2
  • 13

2 Answers2

1

This is to make sure inside that function scope, the $ variable is referencing jQuery.

Imagine some other vendor library or script on the same page was to use the $ variable:

$ = 42
...

(function() {
    console.log($) //--> 42
})()

however, if you pass the global jQuery as a parameter like in your snippet:

$ = 42
...

(function($) {
    console.log($) //--> ƒ (a,b){return new n.fn.init(a,b)} (this is jQuery)
})(jQuery)

It will still reference jQuery. This is because inside the function scope, arguments take precedence over outer scope variables with the same name.

to learn more about scopes in javascript, check this very good article

Halim
  • 527
  • 4
  • 10
0

It's useful when you want to be sure that $ in your anonymous self-invoking function equals to Jquery and not to another library.

Roman Unt
  • 893
  • 7
  • 8