I saw many js minified files having the structure:
(function($,window) ...... )(jQuery,this);
what is with this structure? It's like divided into 2 pairs of parantheses, the very big first one, and the (jQuery,this); in the end.
I saw many js minified files having the structure:
(function($,window) ...... )(jQuery,this);
what is with this structure? It's like divided into 2 pairs of parantheses, the very big first one, and the (jQuery,this); in the end.
It's called an immediately executed function, and is used in this case as a closure to encase all code within a scope, rather than polluting the global environment.
Like any function, an immediately executed one can receive arguments. So it is here; the first argument is the global jQuery
object, while the second one is the outer context, window
.
It's an immediately-invoked function expression. Very common in JavaScript.
(function($,window) ...... )(jQuery,this);
The whole thing is IIFE which sends jQuery
object to be used as a dollar sign inside the IIFE
The other parameter is (in browser environment) the window
object which is this
(in global scope).
The main goal is to use $
and window
inside the function without the fear that something was overwritten.
It is all done in the IIFE which creates its own scope which is not polluting the global scope.
Also
There is another trick to send nothing as an undefined parameter