abidibo.net

Private static and instance members in mootools classes

javascript mootools patterns programming

Let's see two patterns that may be used to have private members in mootools classes.

Private static members

Here is quite simple, it's enough to use a single execution function and js closures and provide the setter and getter methods to change and retrieve the member's values.

var myclass = (function() {
  var _name = null,
      _age = null;
  return new Class({
    initialize: function() {
      // do some stuffs
    },
    setName: function(name) {
      // do some checks here
      _name = name;
    },
    setAge: function(age) {
      // do some checks here
      _age = age.toInt();
    },
    getName: function() {
      return _name;
    },
    getAge: function() {
      return _age;
    }
  });
}());
var myinstance = new myclass();
console.log(myinstance.name); // undefined
myinstance.setName('jack');
console.log(myinstance.name); // undefined
console.log(myinstance.getName()); // jack

So what happens here is that the two variables

_name and _age

are in the scope of the single execution function, and so not accessible from outside. But their value is retained by the returned object which yes can access them since it is inside the single execution function, so it may set and get the variables values and make them accessible through these getter and setter methods.

But what is important to notice here is that such private members are class members or static members, since are shared by all class instances!

Try yourself:

var myinstance1 = new myclass();
var myinstance2 = new myclass();
myinstance1.setName('jack');
console.log(myinstance1.getName()); // jack
myinstance2.setName('jeff');
console.log(myinstance2.getName()); // jeff
console.log(myinstance1.getName()); // jeff !!!!

So, if you want instance members you have to add some tricks

Private members

Here the trick is to make our private variable properties of an object inside a 'class id' property which keep the reference to the instance, let's see the code

var ids = 0;
var myclass = (function() {
  var _private = {};
  return new Class({
    initialize: function() {
      // do some stuffs
      this.id = ids++;
      _private[this.id] = {};
    },
    setName: function(name) {
      // do some checks here
      _private[this.id].name = name;
    },
    setAge: function(age) {
      // do some checks here
      _private[this.id].age = age.toInt();
    },
    getName: function() {
      return _private[this.id].name;
    },
    getAge: function() {
      return _private[this.id].age;
    }
  });
}());
var myinstance1 = new myclass();
var myinstance2 = new myclass();
myinstance1.setName('jack');
console.log(myinstance.getName()); // jack
myinstance2.setName('jeff');
console.log(myinstance2.getName()); // jeff
console.log(myinstance1.getName()); //jack 

Every instance creates a new property of the object _private and stores its private members inside it, clearly is very important here to generate a unique class id.

The next time we'll see how to implement protected properties in mootools class inheritance, using a similar pattern.

Subscribe to abidibo.net!

If you want to stay up to date with new contents published on this blog, then just enter your email address, and you will receive blog updates! You can set you preferences and decide to receive emails only when articles are posted regarding a precise topic.

I promise, you'll never receive spam or advertising of any kind from this subscription, just content updates.

Subscribe to this blog

Comments are welcome!

blog comments powered by Disqus

Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured