Skip to content

Instantly share code, notes, and snippets.

@ldco2016
Created December 25, 2017 23:32
Show Gist options
  • Select an option

  • Save ldco2016/b1b04ea66b2fca16d276e3940eeeda1a to your computer and use it in GitHub Desktop.

Select an option

Save ldco2016/b1b04ea66b2fca16d276e3940eeeda1a to your computer and use it in GitHub Desktop.
IIFE allows us to have data privacy because it creates a new scope that is not visible from the outside scope.
// immediately invoked function expression
// its an anonymous function wrapped in parentheses
// Step 1: JS Runtime executes this line and this anonymous
// function is declared and immediately called because
// of (); operator below.
var budgetController = (function() {
// Step 2: Then this is declared and it returns an
// object below it.
// this part of the code is private
var x = 23;
// this is a private function
var add = function(a) {
return x + a;
}
// this part of the code is public
// return an object containing a method
return {
publicTest: function(b) {
// calling the add function with b
console.log(add(b));
}
}
// invoking the function
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment