Using ECMAScript 5 Getters and Setters With NodeJS
By Kip Lawrence, published 2010-09-24
Did you know you could use ECMAScript 5 features in NodeJS? You can! Try this getter and setter example:
var obj = (function() {
var a;
return {
get a() {
return a;
},
set a(v) {
a = 'prepender: ' + v;
}
};
})();
obj.a = "Hi";
console.log(obj.a);
Running the code gives you the following: [text light="true"] $ node test1.js prepender: Hi [/text] In the obj function you can not access the a variable directly. Instead you have to access it through the getter and setter. You could use code like this to validate your data before you set a, etc... How cool it this!