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.
- Hides the implementations
- simplifies the API (in the example below it only shows the facade function)
- Namespacing > Anonymous Function
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
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


