Private objects in MooTools 1.3+ classes

I spent the last couple of days exploring a way to have private or protected properties in MooTools classes. Various articles (i.e., Sean MacArthur Getting private variables in the MooTools class ) provide an approach for legacy versions of MooTools, but I have not been able to track the working method for MooTools 1.3+.

Today, after playing with the code for several hours, I think . I have created a suitable solution. I say “think” because I'm really not as experienced as a programmer. I was hoping the community here would check my code and tell me if this is really a valid solution or hacking emulation.

var TestObj = (function() {

 var _privateStaticFunction = function() { }

 return new Class({

  /* closure */
  _privates: (function() {

   return function(key, val) {

    if (typeof(this._data) == 'undefined') this._data = {};

    /* if no key specified, return */
    if (typeof(key) == 'undefined') return;

    /* if no value specified, return _data[key] */
    else if (typeof(val) == 'undefined') {
      if (typeof(this._data[key]) != 'undefined') return this._data[key];
      else return;
    }

    /* if second argument, set _data[key] = value */
    else this._data[key] = val;

   }

  /* tell mootools to hide function */
  })().protect(),

  initialize: function() {},

  get: function(val) { return this._privates(val); },
  set: function(key,val) { this._privates(key,val); }

 })

})();


obj1 = new TestObj();
obj2 = new TestObj();

obj1.set('theseShoes','rule');
obj2.set('theseShoes','suck');

obj1.get('theseShoes') // rule
obj2.get('theseShoes') // suck
obj1._privates('theseShoes') // Error: The method "_privates" cannot be called
obj1._privates._data // undefined
obj1._privates.$constructor._data // undefined

I really appreciate any advice! Thank you all!

EDIT: , . , obj1._data. , ! , . , !

+5
1

. .

var - . .

: .

var testObj = (function() {
    var data = {__proto__:null}; // 100% private

    return new Class({
        get: function(key) {
            return data[this.uid][key] || null;
        },
        set: function(key, value) {
            data[this.uid][key] = value;
        },
        remove: function(key) {
            delete data[this.uid][key];
        },
        otherMethod: function() {
            alert(this.get("foo"));
        },
        initialize: function() {
            this.uid = String.uniqueID();
            data[this.uid] = {};
        }
    });
})(); // why exec it?


var foo = new testObj();
var bar = new testObj();

foo.set("bar", "banana");
console.log(foo.get("bar")); // banana!
console.log(bar.get("bar")); // undefined.
bar.set("bar", "apple");
console.info(foo.get("bar"), bar.get("bar")); // banana apple

: http://jsfiddle.net/dimitar/dCqR7/1/

, , this.

, :

http://jsfiddle.net/dimitar/dCqR7/2/

var testObj = (function() {
    var data = {__proto__:null}; // 100% private

    return new Class({
        get: function(key) {
            return data[key] || null;
        },
        set: function(key, value) {
            data[key] = value;
        },
        remove: function(key) {
            delete data[key];
        },
        otherMethod: function() {
            alert(this.get("foo"));
        }
    });
});


var foo = new new testObj();
var bar = new new testObj();

foo.set("bar", "banana");
console.log(foo.get("bar")); // banana!
console.log(bar.get("bar")); // undefined.
bar.set("bar", "apple");
console.info(foo.get("bar"), bar.get("bar")); // banana apple

, ...

mootools , js , , , ..

, , data - . "" , data .

, , . Class... new Class({}); new new <var>() .

, , , - , , :

new (new Class({
    initialize: function() {
        alert("hi");
    }
}))();

, , ( ):

var foo = new Class({
    initialize: function() {
        alert("hi");
    }
});

new foo();

, , , ...

+4

All Articles