Javascript encapsualtion via the module pattern


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var MODULE = function () {
  2. var privateVar = 4
  3.  
  4. var privateMethod = function(arg) {
  5. //body here.
  6. };
  7.  
  8. return {
  9. publicMethod : function(arg) { //so when you instantiate this module, you get a single method back. Could return a complex object via json instead here though that relies on the private state.
  10. privateMethod(arg);
  11. },
  12. };
  13. }();

URL: http://www.infoq.com/presentations/JavaScript-Functions

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.