Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 995 Bytes

execute-code-privately-using-iifes.md

File metadata and controls

34 lines (24 loc) · 995 Bytes

Execute code privately using IIFEs

Immediately Invoked Function Expressions (IIFEs) create a private context that is useful to avoid polluting the global namespace. One can also choose to make specific variables or functions defined within an IIFE public by deliberately exposing them to the global namespace.

An IIFE runs as soon as it is defined:

// this function is immediately invoked
(function () {
  // private variables and functions
})();

Variables and functions defined within an IIFE will be discarded after the IIFE is executed.

If you want to expose a function or variable to the global namespace, return it from the IIFE:

var exposed = (function () {
  var privateVariable;

  function privateFunction() {
    // some code
  }

  function publicFunction() {
    // some code, potentially calling privateFunction()
  }

  return { publicFunction };
})();

After the IIFE has been executed, you can call the exposed function as exposed.publicFunction().