What are Immediately-Invoked Function Expression ?

IIFE for short, This is the self-invoking anonymous function. It executes immediately after it’s created.

Defining IIFE
Syntax : (function(){…})()

The first pair of parentheses (function(){…})() turns the code within into an expression,
and the second pair of parentheses (function(){…})() calls the function that results from that evaluated expression.

This pattern is often used when trying to avoid polluting the global namespace,
because all the variables used inside the IIFE are not visible outside its scope.

Passing parameter to IIFE
Syntax : (function(obj){…})(object);

Here the  (function(obj){…})(object) is passed as a parameter and will be accessible within the function as (function(obj){…})(object).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.