Objective JS with IIFE
Tags: javascript, webdev, node
Published: 2020-09-21 06:54:14

# JavaScript is a Multi-Flavored Beast

As a C# developer; an [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming) developer; a developer who likes encapsulation and re-usability, **Vanilla JavaScript**, and all its wonderful _uses_ scared me - until I stumbled upon the simple concept of an [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) - Immediately Invoked Function Expression.

# Some OOP in the JavaScript Sauce

![sauce](https://media.giphy.com/media/xT39D2A6TB8D1Ce79C/giphy-downsized.gif)

There are so many ways to do things in JavaScript, it can be a little overwhelming for an OOP developer. Where are the classes? Where is the encapsulation? It's all so functional - and so very against the _nature_ of OOP.

IIFEs may not be good (or bad) but they give a way to bring order to _Vanilla JavaScript_, without having to use any frameworks or clever packages. They're relatively simple, and easy to understand - as will be seen in the example. IIFEs are by no means the correct way to do this - just simply a way that brings a little bit of order to the _creative_ chaos.

# The Simplest Example

```
// Reusable modules
var someModule = (function() {
  // private "members" - Here we use closure to ensure that only the object being returned can access these members
  var privateVar = 'A private variable';

  function privateMethod() {
    console.log('This is a private method')
  };

  return {
    // Public variables
    PublicVar: 'A public variable',
    
    // Public methods
    publicMethod() {
      console.log('A public function');
    },
    usingPrivateVariables() {
      console.log('Using a private variable')
      console.log(privateVar);
    },
    usingPrivateMethod() {
      console.log('Calling a private method')
      privateMethod();
    }
  };

}());
```
## Does it work _as Advertised_?
```
someModule.publicMethod();
// Output: 'A public function'

someModule.usingPrivateVariables();
// Output: 'using a private variable'
// Output: 'A private variable'

someModule.usingPrivateMethod();
// Output: 'Calling a private method'
// Output: 'This is a private method'

console.log(someModule.PublicVar)
// Output: 'A public variable'

console.log(someModule.privateVar)
// Output: undefined
// It's private :)

console.log(someModule.privateMethod())
// Output: someModule.privateMethod is not a function
// It's private too!
```

# IIFE or _Iffy_?

![interesting](https://media.giphy.com/media/c4Nc0v0g15g9G/source.gif)

With modern frameworks, IIFEs are a little redundant (or _Iffy_) - the likes of Angular, Vue, and React (to name a few) with their structured way of doing things makes the concept a little obsolete. 

However I've found IIFEs to be wildly useful in older projects - like ASP.NET MVC, especially with JQuery added to the mix. You can abstract out the actual JQuery calls to separate modules; create private and public variables and functions, and do this all in a modular way. IIFEs add a level of re-usability to code in a way that makes sense to my OOP oriented mind.

They've helped me tremendously in untangling otherwise very complex _blotches_ of JavaScript - and I hope they may help some other poor souls struggling with similar issues.