Skip to content

Instantly share code, notes, and snippets.

@brainwire
Created December 21, 2015 05:01
Show Gist options
  • Save brainwire/e2b3d07fdf0791ced723 to your computer and use it in GitHub Desktop.
Save brainwire/e2b3d07fdf0791ced723 to your computer and use it in GitHub Desktop.
Module Design Pattern
var HTMLChanger = (function() {
var contents = 'contents'
var changeHTML = function() {
var element = document.getElementById('attribute-to-change');
element.innerHTML = contents;
}
return {
callChangeHTML: function() {
changeHTML();
console.log(contents);
}
};
})();
HTMLChanger.callChangeHTML(); // Outputs: 'contents'
console.log(HTMLChanger.contents); // undefined
var Exposer = (function() {
var privateVariable = 10;
var privateMethod = function() {
console.log('Inside a private method!');
privateVariable++;
}
var methodToExpose = function() {
console.log('This is a method I want to expose!');
}
var otherMethodIWantToExpose = function() {
privateMethod();
}
return {
first: methodToExpose,
second: otherMethodIWantToExpose
};
})();
Exposer.first(); // Output: This is a method I want to expose!
Exposer.second(); // Output: Inside a private method!
Exposer.methodToExpose; // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment