stuart-d2
1/14/2015 - 3:41 PM

MODULES Revealing Module Pattern □ Similar to above but a singleton by declaring the module and wrapping it inside an IFEE

MODULES Revealing Module Pattern □ Similar to above but a singleton by declaring the module and wrapping it inside an IFEE

/**********Description *****************

Within the return, TWO API functions are exposed -- drive and print.   
The exposed function do not need the same names as the inner function implementation .  
You can also see that the IFEE creates a singleton implementation here :  {("bmw:));  
	• PROS
		® Hiddes implemenation and exposes only the apl
		® F{} aren't part of the global scope
		® It is a cleaner  way  to expose the public interface.  
		® You have only a single instance of the module
	• CONS
		® Ability to extend modules is much harder
		® Debugging could be difficult.  

*/


/**********Basic Template ******************/
var Module = (function () {
    // private variables
    // private functions
        return {
          // public members and functions
};
}());


/***********************Example *************************/

				var car = (function(type) {
				    // private variables
				      var speed = 0;
				      var type = type || "No type";
				
				// private functions
				      function printSpeed() {
				      console.log(speed);
				}
				
				return {
				// public members and functions
				    drive: function(newSpeed) {
				    speed = newSpeed;
				    printSpeed();
				},
				print: printSpeed
				};
				//the singleton
				}("bmw"));
				
				// Later in code
				car.drive(60);