Although I haven’t need them for my classes, I thought it would be very cool to have the possibility to use them. Setters and Getters are very popular in modern programming languages, since they let you do some actions when a member/property of an object is being accessed or modified. Setters and Getters are native on Javascript objects.
-
// Setter on plain JS
-
var b = {
-
saveCount: 0,
-
set count (c) {
-
console.log('Setting count (B):' + c);
-
this.saveCount = c;
-
},
-
get count () {
-
console.log('count is being accessed (B)');
-
return this.saveCount;
-
}
-
}
-
b.count = 5;
-
console.log("Count (B): " + b.count);
Well, Mootools objects created using Class do not support that syntax. Fortunately, we can use the __defineSetter__ and __defineGetter__ functions:
-
// Setter in Mootools
-
var A = new Class({
-
saveCount: 0,
-
initialize: function() {
-
this.__defineSetter__("count", this.setCount);
-
this.__defineGetter__("count", this.getCount);
-
},
-
setCount: function(c) {
-
console.log('Setting count(A):' + c);
-
this.saveCount = c;
-
},
-
getCount: function() {
-
console.log('count is being accessed (A)');
-
return this.saveCount;
-
}
-
});
-
-
var a = new A();
-
a.count = 5;
-
console.log("Count(A): " + a.count);
Nice, uh?

Thank you!
Helped a lot!