Created
December 25, 2017 23:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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