O'Reilly logo

jQuery Design Patterns by Thodoris Greasidis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Revealing Module Pattern

The Revealing Module Pattern is a variant of the Module Pattern with a known and widely recognized name. What makes this pattern special is that it combines the best parts of the Object Literal Pattern and the Module Pattern. All the members of the Module are declared inside an IIFE, which at the end, returns an Object Literal containing only the public members of the Module and is assigned to the variable that acts as our Namespace:

var simpleguid = (function() { 
  var guid = 1; 

  function init() { 
    guid = 1; 
  } 

  function increaseCounter() { 
    guid++; 
  } 

  function getNext() { 
    var nextGuid = guid; 
    increaseCounter(); 
    return nextGuid; 
  } 

  return { 
    init: init, 
    getNext: getNext 
  }; 
})(); 

One of the main benefits of this pattern that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required