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
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

No Reader comments

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!