0
(function($,window,undefined){
...
})(jQuery,this)

I often see codes like above. I understand it passes jQuery to the anonymous function as $ so inside it jQuery can be referred to by $. But what is the purpose for passing window while window is a global variable which you can call anywhere? And why define an undefined parameter while its not being used at all?

shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • That is a matter of preference in passing those parameters, since those are global objects, that can be accessed inside that IIFE without passing and receiving it. – Rajaprabhu Aravindasamy Apr 07 '16 at 16:56
  • @RajaprabhuAravindasamy It is not only a preference. It encapsulates the use of global variables which results in a more robust piece of code. – Tim Apr 07 '16 at 16:57
  • 1
    @Tim Yeah that is for sure. But I was telling about the passing and receiving the global objects inside of it. `(function(){ var jQ = $; var window = this; })();` This is also possible. That is why I told that that was a matter of preference. – Rajaprabhu Aravindasamy Apr 07 '16 at 17:02

0 Answers0