IBdr
IBdr

Reputation: 1

object property function to generate setter and getter

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

Upvotes: 0

Views: 1167

Answers (1)

Lzh
Lzh

Reputation: 3635

Edit (now that I understand OP's intention)

Defining properties that wrap existing fields on an object.

  1. Go through the fields
  2. Use the field names to add a single property for each field.

Like this, see the comments in the code:

let myObj = {
    id: 1,
    name: 'tomato',
};

for(let fieldName of Object.keys(myObj)) {
    console.log(`fieldName = ${fieldName}`);
    Object.defineProperty(myObj, fieldName + "_property", {
        set: function(value) {
            this[fieldName] = value;
        },
        get: function() {
            return this[fieldName];
        }
    });
}

myObj.id_property = 55; // setting id_property will set id
console.log(myObj.id); // so this will write 55

myObj.id = 123; // setting id
console.log(myObj.id_property); // this will write 123 because id_property returns id field

[Obsolete("Previous answer")]

Before trying a generic approach to defining properties, let's try to define a normal static property. For example here is code for property called Xyz on some object called myObj.

As you can see, you can define the getter and setter in a single call to defineProperty.

var myObj = {};
Object.defineProperty(myObj, `Xyz`, {
    set: function (value) {
        this['_Xyz'] = value;
    },
    get: function() {
        return this._Xyz;
    }
});

Also notice that I'm using a "backing field" called _Xyz here.

So to make things generic, we want to define a property called "Something" and use a backing field called "_Something".

You can easily then create a loop for this.

Then when you run the loop you can access properties like the above like this:

myObj.Xyz = 1; console.log(myObj.Xyz); // writes 1


Also notice that when you set and get a property, you do NOT use parentheses. E.g. myObj.myProperty() is NOT correct. Conventionally, we also don't add get or set prefixes to the name of the property. We just say myProperty and not getMyProperty.

Upvotes: 2

Related Questions