Skip to content

Instantly share code, notes, and snippets.

@thedapifer
Created March 6, 2018 03:57
Show Gist options
  • Select an option

  • Save thedapifer/ece8a044c9db808537327c180b871cb3 to your computer and use it in GitHub Desktop.

Select an option

Save thedapifer/ece8a044c9db808537327c180b871cb3 to your computer and use it in GitHub Desktop.
JQuery Module Pattern
  1. 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.

  1. 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.

  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment