mootools protected members
Just a demonstration of how protected members may be implemented using mootools
by abidibo
HTML
<div id="test_result">risultati:
</div>
JavaScript
var ids = 1;
var myParentClass = (function(){
var _protected_prop = {};
return new Class({
initialize: function(id, par1, par2) {
this.id = id;
_protected_prop[this.id] = {};
_protected_prop[this.id].prop1 = 'myvalue1';
_protected_prop[this.id].prop2 = true;
_protected_prop[this.id].prop3 = 65;
},
protectedProp: function() {
return _protected_prop[this.id];
}.protect(),
changeProp: function(value) {
_protected_prop[this.id].prop1 = value;
}
});
}());
var myChildClass = (function() {
var _protected_prop = {};
return new Class({
Extends: myParentClass,
initialize: function(par1, par2, par3) {
var id = ids++;
this.parent(id, par1, par2);
// here the protected prop of the parent are ready
_protected_prop[this.id] = {
prop4: 'myvalue4',
prop1: 'mayvalue1childaltered'
};
_protected_prop[this.id] = Object.merge(this.protectedProp(), _protected_prop[this.id]);
},
test: function() {
$('test_result').appendText(_protected_prop[this.id].prop1 + ' ');
}
});
}());
var c = new myChildClass(1, 2, 3);
c.test();
c.changeProp('myalteredfromparentvalue');
c.test();