MODULES Revealing Prototype Pattern
/* DETAILS
• Same pattern as prior, but on a prototype.
• A singleton prototype that exposes some functionality
• That singleton then meets a constructor that uses the singleton as its prototype
• PROS
○ hides the implemenation and exposes only APL
○ F{} and vars arent a part of the global scope
○ F{} are loaded into memory only once and not for every instance
○ It is extensible (though I do not think that is shown here, would need see above MONO examples and test for myself, extending)
• CONS
○ this is tricky
○ prototype is defined separately from the constuctor function.
*/
/***************Basic Template *******************************/
var Module = function () {
// private/public variables
// private/public functions
};
Module.prototype = (function() {
//private implementation
return {
// public API
};
}());
/***************Example *******************************/
var Car = function(type) {
this.speed = 0;
this.type = type || "no type";
}
Car.prototype = (function() {
//private functions
car printSpeed = function() {
console.log(this.speed);
}
var drive = function(newSpeed) {
this.speed = newSpeed;
printSpeed.call(this);
}
return {
// public members and functions
drive : drive
};
}());
//constructor for new instance -- bmw.
var car = new Car("bmw");
car.drive(60);