Using closures in javascript, we can create public and private methods.

revealing

Only the methods and variables that are specifically returned are available publicly and we can provide references to privately declared methods and variables inside the public methods.

Layout for RMP:

var applicationModule = (function() {
    //private variable. Existence limited within
    //this closure
    var privateVariable = 10;
    var privateFunction = function() {
    //Not accessible publicly
    };
    //accessibly publicly as
    //applicationModule.publiclyReturnPrivateVar
    var publicFunction = function() {
    return privateVariable;
    };
    return {
    publiclyReturnPrivateVar: publicFunction
    };
})();
 
//prints value of privateVariable
console.log(applicationModule.publiclyReturnPrivateVar());
Last modified: September 14, 2018

Author