Created
December 4, 2014 22:14
-
-
Save benwillkommen/dc5c325068cae6fe93eb to your computer and use it in GitHub Desktop.
This file contains 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
//the variable "oversimplifiedModule" gets assigned the return value of the anonymous and self executing function | |
var oversimplifiedModule = (function(){ | |
//"private" variables are declared within the scope of the anonymous function: "counter" will not be in the global scope. | |
var counter = 42; | |
//the return value of the anonymous function can be considered the modules "public" interface | |
return { | |
incrementCounter: function(){ | |
//because we refer to "counter", which is in the outer scope, inside this inner scope, a closure is formed, which keeps a reference to "counter" | |
//and prevents it from being garbage collected. closures are very neat and useful. | |
counter++; | |
}, | |
printCounter:function(){ | |
//just an example of another "public" member | |
console.log(counter); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment