Skip to content

Instantly share code, notes, and snippets.

@benwillkommen
Created December 4, 2014 22:14
Show Gist options
  • Save benwillkommen/dc5c325068cae6fe93eb to your computer and use it in GitHub Desktop.
Save benwillkommen/dc5c325068cae6fe93eb to your computer and use it in GitHub Desktop.
//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