Given the following:
x = {
aInternal: 10,
aListener: function(val) {},
set a(val) {
this.aInternal = val;
this.aListener(val);
},
get a() {
return this.aInternal;
},
registerListener: function(listener) {
this.aListener = listener;
}
}
x.registerListener(function(val) {
console.log("Someone changed the value of x.a to " + val);
});
x.a = 42; //Writes to the console that the variable has changed
How can I use an array of listeners so that I have, x.a
, x.b
and so-on?