Skip to content

Instantly share code, notes, and snippets.

@nmoliveira
Created May 18, 2013 23:00
Show Gist options
  • Save nmoliveira/5606029 to your computer and use it in GitHub Desktop.
Save nmoliveira/5606029 to your computer and use it in GitHub Desktop.
A example of the javascript Revealing Module Pattern
var Calculator = function() {
// private members goes here
var privateLog = false;
var privateConfig = function(shouldLog) {
privateLog = shouldLog;
};
var privateAdd = function (x,y) {
var result = x + y;
if (privateLog) {
console.log(result);
}
return result;
};
var privateSubtract = function (x,y) {
var result = x - y;
if (privateLog) {
console.log(result);
}
return result;
};
// public members goes here
return{
publicConfig: privateConfig,
publicAdd: privateAdd,
publicSubtract: privateSubtract
};
}();
Calculator.publicConfig(true);
Calculator.publicAdd(1,2); // logs 3
Calculator.publicSubtract(2,1); // logs 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment