person = {
id: 1,
name: 'John',
phone: '123-456-789',
getSetGen: function(){
for(var key in this){
this[`set${key}`]=function(value){
this[key] = value;
console.log(this[key]);
}
this[`get${key}`]=function(){
console.log(this[key]);
return this[key];
}
// for(key in this){
// console.log(this[key]);
// if(key!='getSetGen'){
// Object.defineProperty(this, `Set${key}`, {
// set : function (value) {
// this[key] = value;
// }
// });
// Object.defineProperty(self, `Get${key}`, {
// get : function () {
// return this.key;
// }
// });
// }
// }
}
}
person.getSetGen()
person.getname();
I am trying to make a function property to generate getters and setters for the object and can used with other object.
After running the code above the setter and getter is not working, just carrying the functions as 'this[key]' not the actual properties and object