What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

In JavaScript, wrapping the entire content of a source file generally comes with immediately invoking it. The function is called an Immediately Invoked Function Expression, or IIFE. It is also called a Self-Executing Anonymous Function.

(function ()…


This content originally appeared on DEV Community and was authored by Abdullah Numan

In JavaScript, wrapping the entire content of a source file generally comes with immediately invoking it. The function is called an Immediately Invoked Function Expression, or IIFE. It is also called a Self-Executing Anonymous Function.

(function () {
  /* … */
})();

It's a design pattern commonly used in ES6 Module Pattern. It helps us encapsulate data and functionality in a JS source file, i.e. a node module such as React, Redux, etc.

IIFEs are significant because:

  • they help with namespacing functions and variables in a library. Namespacing helps us prevent polluting the global namespace.
  • they help us control privacy of variables and functions. With an IIFE, we are able to expose only the APIs that we want to -- by returning them in an object, and hide the rest.

References

  1. IIFE
  2. Does the module pattern require the use of an IIFE?


This content originally appeared on DEV Community and was authored by Abdullah Numan


Print Share Comment Cite Upload Translate Updates
APA

Abdullah Numan | Sciencx (2022-07-17T04:03:22+00:00) What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?. Retrieved from https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/

MLA
" » What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?." Abdullah Numan | Sciencx - Sunday July 17, 2022, https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/
HARVARD
Abdullah Numan | Sciencx Sunday July 17, 2022 » What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?., viewed ,<https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/>
VANCOUVER
Abdullah Numan | Sciencx - » What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/
CHICAGO
" » What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?." Abdullah Numan | Sciencx - Accessed . https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/
IEEE
" » What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?." Abdullah Numan | Sciencx [Online]. Available: https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/. [Accessed: ]
rf:citation
» What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block? | Abdullah Numan | Sciencx | https://www.scien.cx/2022/07/17/what-is-the-significance-of-and-reason-for-wrapping-the-entire-content-of-a-javascript-source-file-in-a-function-block/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.