Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefit of Immediately-invoked function expression (IIFE) over a normal function

Tags:

I'm pretty new to javascript and I read about the module pattern to provide some sort of namespace and have both private and public members, for example:

var module = (function() {    var s = "Hello, i'm private and in closure!";    return {       myString : s,       myFunc: function() { alert(s); }    }; })(); 

I do see the benefits of that, because it gives you some of the advantages of object-oriented programming. But I've seen a lot of examples of an IIFE that doesn't get assigned to a variable. This (as far as I see) has no advantages at all compared to a normal function that you invoke:

1. IIFE

(function() {    var s = "Hello I'm private!";    $('#myButton').on('click', function() {       alert(s);    }); })(); 

2. Normal function

function Initialize() {    var s = "Hello I'm private!";    $('#myButton').on('click', function() {       alert(s);    }); }  Initialize(); 

They both have private variables that avoid the need of creating global variables and they both execute without returning any value to a variable. Although the second one gives you the option of choosing a good name that says a lot more than a potential large IIFE without the name, leaving the reader to find out what's happening. The answer I see everywhere is 'to avoid namespace pollution' but both approaches do that, the first one is just a bit harder to understand?

In short:

What is the benefit of using an IIFE over a normal function that I'm missing? Why should I use them?

like image 393
Alexander Derck Avatar asked May 04 '16 07:05

Alexander Derck


People also ask

What is IIFE immediately invoked function expression in JS?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

What is the benefit of function expression in JavaScript?

There are some benefits of function expressions over statements, for example they are anonymous functions, they can be used as closures, as arguments to other functions and as IIFEs. Put simply a statement in the execution phase it does not return anything, an expression results to a value, an object is created.

What is anonymous and IIFE function?

A function's name is the string of characters that come in between the function keyword and the set of parentheses () . The first function has a name, declaredFunction , but the second function, a function expression, does not have a name. The function without a name is called an anonymous function.

What is the valid syntax of declaring IIFE function?

The following expression is called an immediately invoked function expression (IIFE) because the function is created as an expression and executed immediately: (function(a,b){ return a + b; })(10,20); Code language: JavaScript (javascript) This is the general syntax for defining an IIFE: (function(){ //...


1 Answers

Sometimes you need to define and call function at the same time and only once so in this case anonymous function helps you. In such situations giving functions a name and then calling them is just excess.

Further sometimes you wants to create a namespace for your variables. So anonymous functions helps you there too. For example

(function($) {     $.fn.pluginName = function(opt) {         // implementation goes here...     } }(jQuery)); 

In above case you can safely use $ as jQuery synonym in your code.

If you define a function with name as shown below, then it will create global variable with function name as you defined.

function myFunction() {     // function code goes here. } myFunction(); 

But if you define it without name then it won't create any global variable and your global namespace will not be polluted.

(function myFunction() {     // function code goes here. }()); 

Function with names are useful only when you need to call them from different places in your code.

like image 77
Mohammad Usman Avatar answered Nov 15 '22 11:11

Mohammad Usman