Oct 10 2008

0

Mootools Privates Class Mutator

Category: Javascript, MootoolsTags:

I think I found a working pattern to implement private properties and methods within your classes. This could be useful in protecting the integrity of your class. Making sure that the public interface defined is used and nothing more.

Class.Mutators.Privates = function(self,properties){
	var priv = {};
	$splat(properties).each(function(property){
		priv[property] = self[property];
	});
	self.initalize = (function(){
		for(prop in priv){ this[prop] = priv[prop]; self[prop] = undefined; }
		return self.initialize;
	})();
}

Example :

var Secret = new Class({
	Privates : ['secret'],
	secret : 'hidden message',
 
	getSecret : function(){
		return secret;
	}
 
});
 
var msg = new Secret();
msg.getSecret(); // returns "hidden message"
msg.secret; // returns undefined

These is a proof of concept for now. I haven’t used it other with some simple test cases. I will explore this further and report my findings.

Tags:

Leave a Reply