Module and Revealing Module Patterns
suggest changeModule Pattern
The module pattern hides private bindings and returns a public API. An IIFE keeps those bindings in a closure.
This gives us a clean solution for hiding the main logic and only exposing an interface we wish other parts of our application to use.
const Module = (function(/* pass initialization data if necessary */) {
// Private data is stored within the closure
const privateData = 1;
// Because the function is immediately invoked,
// the return value becomes the public API
var api = {
getPrivateData: function() {
return privateData;
},
getDoublePrivateData: function() {
return api.getPrivateData() * 2;
}
};
return api;
})(/* pass initialization data if necessary */);
Revealing Module Pattern
The revealing module pattern is a variant. Private and public functions are both declared in the closure. The returned object only names them. Internal calls use those names directly, not the returned object.
const Module = (function(/* pass initialization data if necessary */) {
// Private data is stored just like before
const privateData = 1;
// All functions must be declared outside of the returned object
const getPrivateData = function() {
return privateData;
};
const getDoublePrivateData = function() {
// Refer directly to enclosed members rather than through the returned object
return getPrivateData() * 2;
};
// Return an object literal with no function definitions
return {
getPrivateData: getPrivateData,
getDoublePrivateData: getDoublePrivateData
};
})(/* pass initialization data if necessary */);
Revealing Prototype Pattern
This variation puts constructors on a namespace and methods on a prototype. The sample below needs jQuery ($). Replacing prototype with a plain object drops constructor.
//Namespace setting
var NavigationNs = NavigationNs || {};
// This is used as a class constructor
NavigationNs.active = function(current, length) {
this.current = current;
this.length = length;
}
// The prototype is used to separate the construct and the methods
NavigationNs.active.prototype = function() {
// It is a example of a public method because is revealed in the return statement
const setCurrent = function() {
// this.current and this.length are public instance properties.
// The loop adds the class to the same element once per count.
for (let i = 0; i < this.length; i++) {
$(this.current).addClass('active');
}
}
return { setCurrent: setCurrent };
}();
// Example of parameterless constructor
NavigationNs.pagination = function() {}
NavigationNs.pagination.prototype = function() {
// reload is private because the returned object does not name it
const reload = function(data) {
// do something
},
// getPage is public because the returned object names it
getPage = function(link) {
const a = $(link);
const options = {url: a.attr('href'), type: 'get'}
$.ajax(options).done(function(data) {
// after the Ajax call finishes, it calls the private function
reload(data);
});
return false;
}
return {getPage : getPage}
}();
This code above should be in a separated file .js to be referenced in any page that is needed. It can be used like this:
const menuActive = new NavigationNs.active('ul.sidebar-menu li', 5);
menuActive.setCurrent();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents