Skip to content

Instantly share code, notes, and snippets.

@eliza-abraham
Created January 27, 2016 11:01
Show Gist options
  • Save eliza-abraham/9d5a3b7e238f16dde221 to your computer and use it in GitHub Desktop.
Save eliza-abraham/9d5a3b7e238f16dde221 to your computer and use it in GitHub Desktop.
Module Pattern in Javascript
/* IIFE - Immediately Invoked Function Execution :
This function calls itself immediately after declaration, it creates a new scope and creates "privacy".
Return only the part we need, while keeping the other functions private.
*/
(function () {
// code goes here
})();
// Adding a namespace
var Module = (function () {
// code goes here
})();
// Creating a private method
var Module = (function () {
var privateMethod = function () {
// do something here
};
})();
// Returning public methods
var Module = (function () {
var privateMethod = function () {
// do something here
};
return {
publicMethod: function () {
// can call privateMethod() here
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment