I am trying to create an object that defines getters/setters automatically for a new instance of an object. I want the setter to put the values in a separate object property called newValues. Why in the following code snippet does setting the value of prop1 actually set the value of newValues.prop2 instead of newValues.prop1?
Am I doing something silly here? It's totally possible as I am on only a few hours of sleep... :)
var Record = function(data) {
this.fieldValues = {}
this._data = data;
var record = this;
for(var key in data) {
record.__defineGetter__(key, function() {
return record._data[key];
});
record.__defineSetter__(key, function(val) {
record.fieldValues[key] = val;
});
}
}
var myRecord = new Record({prop1: 'prop1test', prop2: 'prop2test'});
myRecord.prop1 = 'newvalue';
console.log(myRecord.fieldValues.prop1); // undefined
console.log(myRecord.fieldValues.prop2); // 'newvalue'