I have a class (constructor function) User
function User(id) {
this._id = id;
}
from which I make my objects like user
var user = new User(9);
And I want to write getter function for this user
object, but not actually particularly for it, but rather for all objects created by my constructor function User
.
I though this should work but it isn't
Object.defineProperty(User, 'id', {
get: function() {
console.info('Using getter..');
return this._id;
},
// set:function(val) { alert('set value'); }
});
var user = new User(9);
console.info(user.id); // gives undefined
I figured it's because User
is a constructor function, and not actually an object, defining getter function like that doesn't work. Is it still possible some other way?