I'm trying to create an object that builds its own getters and setters dynamically:
function Person( properties ) { // 'properties' is an object literal
this._private = properties; // private by convention
for ( key in this._private ) {
this[key] = function() {
return this._private[key];
}
}
}
I hoped, this would create something like this:
var jack = new Person({
working:true,
age:33,
gender:'male'
});
jack.working() --> true
jack.age() --> 33
jack.gender() --> 'male'
The problem is, it always returns 'male', like so:
jack.working() --> 'male'
jack.age() --> 'male'
jack.gender() --> 'male'
What am I missing? Oh, and this is just a proof of concept. I know this is not a perfect solution for creating getters and setters in JavaScript.