Skip to content

Instantly share code, notes, and snippets.

@pedrobritto
Created July 16, 2019 19:22
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 pedrobritto/e5adc14070fa306f2f764aca5f376ebe to your computer and use it in GitHub Desktop.
Save pedrobritto/e5adc14070fa306f2f764aca5f376ebe to your computer and use it in GitHub Desktop.
// Module Pattern
// If not an IIFE, it's contents can be read as a string
var Exposer = (function () {
// Everything here is private
var private = 'contents';
// Everything here is public
return {
public: function() {
console.log(private);
}
}
}());
console.log(Exposer.private);
Exposer.public();
// Revealing Module Pattern
var Exposer = (function () {
// Everything here is private
var private = 'contents';
var remainPrivate = function() {
console.log('This will not be revealed');
}
var showThis = function() {
console.log('This will be revealed');
}
var showThisToo = function() {
console.log('This will be revealed as well');
}
// Everything here is public
return {
showThis: showThis,
showThisToo: showThisToo
}
}());
console.log(Exposer.remainPrivate); // Undefined
Exposer.showThis(); // 'This will be revealed'
Exposer.showThisToo(); // 'This will be revealed as well'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment