Petr Volobuev | Technology RadarPetr Volobuev | Technology Radar

IIFE Modules

frontend
Using

I've learned how to write js modules like a decent human being, to play nicer with the shared context. IIFE modules brought order to my code — writing it got noticeably easier.

These modules encapsulate their code inside a (function () { /* ... */ })(); or (() => { /* ... */ })(); construct.

The gist of the approach can be shown with this example.

const Counter = (function () {
  let value = 0;

  function inc() { value++; }
  function get() { return value; }

  return { inc, get };
})();

Counter.inc();
console.log(Counter.get());