Created
January 27, 2016 11:01
-
-
Save eliza-abraham/9d5a3b7e238f16dde221 to your computer and use it in GitHub Desktop.
Module Pattern in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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