Tuesday, September 1, 2015

Javascript Revealing Module Pattern

The Revealing Module Pattern is based on a pattern referred to as the Module Pattern. It makes reading code easier and allows it to be organized in a more structured manner. The pattern starts with code like the following to define a variable, associate it with a function and then invoke the function immediately as the script loads. The final parenthesis shown in the code cause it to be invoked. 
var Calculator = function () {  
    service1 = function() {......}
    service2 = function() {......}
    return {
        myService1: service1,
        myService2: service2
    }
};
You can define which members are publicly accessible and which members are private. This is done by adding a return statement at the end of the function that exposes the public members. 

Source: http://weblogs.asp.net/dwahlin/techniques-strategies-and-patterns-for-structuring-javascript-code-revealing-module-pattern

No comments:

Post a Comment