Reveal Module Pattern
A reveal module is an immediately-invoked function expression that returns an object who's methods have closure over the enclosing functions variable scope.
Breaking down the definition, there are 3 key aspects to reveal-module.
The outer function is executed using a the IIFE syntax (...)();
. This has the benefit of preventing any polution of the global scope with extra unnecessary varaibles.
The outer function returns an object literal with functions as properties (methods). This is a factory that generates an object with methods (functions) and properties (variables).
Those methods create a closure over the scope so the retain (read, remember) the scope where they were defined. This effectively creates private properties and methods.
Some additional resources if you need them.
(function() {
'use strict';
// Your code here
// All function and variables are scoped to this function
}());
var myCalc = (function() {
'use strict';
return {
total: 0,
foo: "bar"
};
}());
console.log(myCalc.total);
console.log(myCalc.foo);
_private
, just in closure.var myCalc = (function() {
'use strict';
var current = 0;
var something = "bar";
return {
total: current,
foo: something
};
}());
console.log(myCalc.total);
console.log(myCalc.foo);
myCalc.foo = "qux";
console.log(myCalc.foo); //notice how you can modify the enclosed variables
var myCalc = (function() {
'use strict';
// I'm protected. can be accessed and changed but only through methods
var _protected = 0;
return {
getPrivate: function(){return _private},
setPrivate: function(val){return _private = val}
};
}());
console.log(myCalc.getPrivate())
console.log(myCalc.setPrivate(10))
console.log(myCalc.getPrivate())
.add()
and .total()
method which work with a private _current
variable.var myCalc = (function() {
'use strict';
var _current = 0; //defaults to zero
return {
add: function(val) {
return _current = _current + val;
},
total: function(val) {
return _current;
}
};
}());
console.log(myCalc.add(5));
console.log(myCalc.total());
console.log()
calls below work properly.var myCalc = (function() {
'use strict';
var _current = 0; //defaults to zero
return {
add: function(val) {
return _current = _current + val;
},
total: function(val) {
return _current;
}
};
}());
console.log(myCalc.add(5)); // should output ==> 5
console.log(myCalc.subtract(3)); // ==> 2
console.log(myCalc.multiply(5)); // ==> 10
console.log(myCalc.divide(2)); // ==> 5
console.log(myCalc.total()); // ==> 5
console.log(myCalc.clear()); // ==> 0
console.log(myCalc.total()); // ==> 0
If you've completed the calculator and would like more practice. Try building the calculator from scratch without the instructions as a guide. Or try your hand at building the Employee Module below.
Private Properties
Private Methods
Public Methods:
Finally, if you've completed all the above and would like a glimpse on how chaining works in jQuery you can modify the calculator to support chaining.
Note: Most jQuery methods actually returns the jQuery object which is a special jQuery set of elements similar to an array. In this way, jQuery can work on multiple elements.
Update the calculator to return the object. Complete the subtract
method so the test returns 2
. And then create your own versions of multiply
, divide
, and clear
Here's a starter version:
var myCalc = (function() {
'use strict';
var _current = 0; //defaults to zero
var _add = function(val) {
_current = _current + val;
return this;
};
var _subtract = function(val) {
// YOUR CODE HERE
};
var _total = function(val) {
return _current;
};
return {
add: _add,
subtract: _subtract,
total: _total
};
}());
console.log(myCalc.add(5).subtract(3).total()) // ==> 2