Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript wrapping code inside anonymous function

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?

like image 805
Mintz Avatar asked Oct 26 '13 18:10

Mintz


People also ask

Can you pass parameters to anonymous function JavaScript?

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.

Does JavaScript support anonymous function?

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.

What is the reason for wrapping the entire content of a JavaScript code in a function block?

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.

What is wrapper function in JavaScript?

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.


2 Answers

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:

  1. 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.

  2. 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.

like image 143
jfriend00 Avatar answered Sep 23 '22 18:09

jfriend00


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.

like image 20
Niet the Dark Absol Avatar answered Sep 23 '22 18:09

Niet the Dark Absol