Skip to content

Instantly share code, notes, and snippets.

@evanhutomo
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanhutomo/f4d0da45fb0cfdc40864 to your computer and use it in GitHub Desktop.
Save evanhutomo/f4d0da45fb0cfdc40864 to your computer and use it in GitHub Desktop.
phase 1 : Learn javascript module pattern
/*
Courtesy of addyosmani
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript
*/
// Counter covers namespace
var Counter = (function(){
//count covers private var
var count = 0;
//count covers private method
var privateMethod;
privateMethod = function(){
console.info("test");
};
// property in return scope will be public
return {
counting: function(pAmount){
var _counter = 0;
for (; count < pAmount; count++) {
_counter += 1;
}
return _counter;
},
show: function(){
return count;
},
reset: function(){
console.log("counter : " + count);
count = 0;
},
thisIsPublicMethod: function(){
privateMethod();
}
}
})();
Counter.counting(5);
console.log(Counter.reset());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment