-2

I am new to Jquery / Js development. I have seen below code block in an sample JS file downloaded from internet.

(function($, undefined) {
...
})(window.jQuery);

Can anyone please explain what this code block is all about? I mean please interpret this code block.

Thanks in advance, BalaGurusamy

1 Answers1

0

That is an immediately invoked function expression (IIFE). The outer parentheses turn the function into an expression instead of a declaration, and the 2nd pair actually invoke it.

The anonymous function accepts 2 arguments, $ and undefined, and you pass in one argument, a reference to the jQuery object. So inside the anonymous function $ will refer to jQuery and since no 2nd argument was provided undefined will contain the undefined value (which can be important since undefined could have been overwritten elsewhere in the script).

James Allardice
  • 164,175
  • 21
  • 332
  • 312