An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.
(function () {
statements
})();
It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts:
- The first is the anonymous function with lexical scope enclosed within the
Grouping Operator
()
. This prevents accessing variables within the IIFE idiom as well as polluting the global scope. - The second part creates the immediately invoked function expression
()
through which the JavaScript engine will directly interpret the function.
Use cases
Avoid polluting the global namespace
Because our application could include many functions and global variables from different source files, it's important to limit the number of global variables. If we have some initiation code that we don't need to use again, we could use the IIFE pattern. As we will not reuse the code again, using IIFE in this case is better than using a function declaration or a function expression.
(function () {
// some initiation code
let firstVariable;
let secondVariable;
})();
// firstVariable and secondVariable will be discarded after the function is executed.
The module pattern
We would also use IIFE to create private and public variables and methods. For a more sophisticated use of the module pattern and other use of IIFE
Advantages of IIFE:
- Do not create unnecessary global variables and functions
- Functions and variables defined in IIFE do not conflict with other functions & variables even if they have same name.
- Organize JavaScript code.
- Make JavaScript code maintainable.
https://www.tutorialsteacher.com/javascript/immediately-invoked-function-expression-iife
No comments:
Post a Comment