- Immediately-Invoked-Function-Expressions
(function () { // code })();
** What does it do? -- It declares a function which in turn calls itself immediately (invokes). -- The function creates a new "scope" and creates "privacy" within itself.
- Namespace var Module = (function () {
var privateMethod = function () { // do something };
})();
** What does it do? -- Allows us to access methods we wish to return.. by "name-space" get it? -- The "module" is now available in the "global" space and we may call it whenever we'd like. We can even pass it into another module.
- Private Methods
var Module = (function () {
var privateMethod = function () { // do something };
})();
** What does it do? -- Protects our function(s) by wrapping them within a private method. Private methods are anything that you don't want public. For example, server side data could be a potential risk when exposed to hackers and other users. -- Also helps with naming conflicts. Very similar to php class method.
-- In the above function "var privateMethod" is declared locally and will return an error if called outside of the "var Module" function method.