JavaScript
Article

The Facade JavaScript Design Pattern

By Sam Deering

Facade: a superficial appearance or illusion of something: They managed somehow to maintain a facade of wealth.

Very interesting design pattern I cam across which provides protection of code and only exposure to those functions which you return from within the encapsulated object. Worth the share.

var module = (function() {  
    var _private = {  i: 5,
          get: function() {  console.log('current value:' + this.i);  
        },
          set: function(val) {  this.i = val;  
        },
          run: function() {  console.log('running');  
        },
          jump: function() {  console.log('jumping');  
        }  
    };
    return {  facade: function(args) {  _private.set(args.val);  _private.get();  
            if (args.run) {  _private.run();  
            }  
        }  
    }
}());
module.facade({ run: true, val: 10 }); //outputs current value: 10, running

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.