Tutorial :Private Methods in a Mootools Class


I'm relatively new to using oop in Javascript, and I'm wondering what the best practice is for private methods. Right now, I'm using mootools to create my classes and I'm simulating private methods by prefixing them with an underscore and forcing myself not to call the method outside of the class. So my class looks like:

var Notifier = new Class(  {     ...     showMessage: function(message) { // public method        ...     },       _setElementClass: function(class) { // private method        ...    }  });  

Is this a good/standard way to handle private methods in JS?


MooTools provides a protect method on functions, so you can call protect on any method that you want to protect from being called outside the Class. So you can do:

​var Notifier = new Class({      showMessage: function(message) {        },      setElementClass: function(klass) {        }.protect()  })​;    var notifier = new Notifier();  notifier.showMessage();  notifier.setElementClass();  > Uncaught Error: The method "setElementClass" cannot be called.  

Not that class is a future reserved keyword in JavaScript and your code may break when using it. It certainly breaks on Safari at this point, but the behavior in other browsers is not guaranteed as well, so it's better to not use class as an identifier at all.

One advantage of using protect over creating closures yourselves is that if you extend this class, you can still access the protected methods in subclasses.

Notifier.Email = new Class({      Extends: Notifier,        sendEmail: function(recipient, message) {          // can call the protected method from inside the extended class          this.setElementClass('someClass');      }  });    var emailNotifier = new Notifier.Email();  emailNotifier.sendEmail("a", "b");  emailNotofier.setElementClass("someClass");  > Uncaught Error: The method "setElementClass" cannot be called.  

If you want to use a naming convention such as prefixing or suffixing _ before or after a method, then that's perfectly fine as well. Or you can combine the _ with the protected methods too.


Well, as long as you stay consistent you won't get into trouble.

There is a pattern though, for creating true privacy in javascript via closure.

var Notifier = function() {        // private method      function setElementClass(class) {           //...      }        // public method      this.showMessage = function(message) {          // ...          setElementClass(...) // makes sense here      };  };    var noti = new Notifier();  noti.showMessage("something");     // runs just fine  noti.setElementClass("smth else"); // ERROR: there isn't such a method  

If you want to add public methods that are inherited and shared between all objects (smaller memory footprint), you should add them to the object's prototype.

// another way to define public functions  // only one will be created for the object  // instances share this function  // it can also be used by child objects  // the current instance is accessed via 'this'  Notifier.prototype.showMessage = function() {     // ...     this.otherPublicFunction(...);  };​  

I recommend you to look into the raw way of handling objects in javascript, because only then you will be able to know what you're doing. Mootools like classes would be good to hide what this language differs from others. But truth is that it differs so greatly that it would be naive to think that you do the same thing when you say class in javascript, like in any other class-based OO language.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »