I need help understanding this pattern for jQuery plugin authoring. Can somebody explain this simple bit of code for me?
(function($) { /* Code here */ })(jQuery);
I know this is to avoid conflict with different plugins using the same $
character but somehow cannot get around my head to understand how it works. What relation does the parameter $
has to the jQuery
object that got parsed in?
The syntax is simple: you can simply declare the anonymous function and make it execute by just calling it using the parenthesis at the end of the function. You can simply pass the parameters inside the immediate execution of the anonymous function as we have seen in the above example.
Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.
The purpose of wrapping is to a namespace and control the visibility of member functions. It wraps the code inside a function scope and decreases clashing with other libraries.
In programming languages such as JavaScript, a wrapper is a function that is intended to call one or more other functions, sometimes purely for convenience, and sometimes adapting them to do a slightly different task in the process. For example, SDK Libraries for AWS are examples of wrappers.
Let's break this down:
(function($) { /* Code here */ })(jQuery);
First, the construct:
(function() {})();
creates an immediately executed function expression (often called IIFE). This is a function that is executed immediately rather than defined now, but called later. It is essentially an anonymous (unnamed) function that is defined and then executed right away.
Then, passing jQuery to it like this:
(function() {})(jQuery);
passes jQuery as the first argument to that immediately executed function. Then, naming that first argument as $
defines that symbol inside the function to correspond to the first argument that is passed.
(function($) {})(jQuery);
which in expanded form looks like this:
(function($) { // you can use $ here to refer to jQuery })(jQuery);
There a couple nice thing about this for jQuery plugin authors:
The IIFE creates a local function context so you can have variables which are "global" for your plug-in, but are not actually global variables and thus don't pollute or conflict with the actual global variable namespace.
You can program with $
for jQuery, whether or not the host program actually has that defined for jQuery because you've defined $
locally within your function.
What you have there is shorthand for something like this:
function anonymous_function($) { // Code here }; anonymous_function(jQuery);
As you can see, it allows the $
symbol to be used as a reference to the jQuery
object inside the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With