DEV Community

Discussion on: Automate your getters and setters with Proxies

Collapse
 
adamkaram profile image
Adam karam

thank u so much but i did change it a quiet little bit to Fits my needs like this
"use strict";

    let _object = {
        id: 'SD-10',
        location: 'Sv',
        addr: '123 str',
        // getSetGen: () => {

        // }
    };

    function log([...param]) {
        var v = param.map(x => console.log(x))


    }


    function getPropertyFromGetterSetter(property) {
        const sliced = property.slice(3);
        const firstLetter = sliced[0].toLowerCase();
        const rest = sliced.slice(1);



        return firstLetter + rest;
    }
    // علشان تحدد اذا كان ال Moethod
    //هتكون getter ولا setter  
    const withGettersSetters = {

        get(object, property) {
            // "getConstructionYear"
            if (property.startsWith("get")) {
                // getSetGen.get x , y  , z ()

                return () => object[getPropertyFromGetterSetter(property)];
            }

            if (property.startsWith("set")) {
                // getSetGen.set y , x ,z(value)
                return (newValue) => {
                    object[getPropertyFromGetterSetter(property)] = newValue;
                };
            }

            // getSetGen. x , y , z 
            return object[property];
        }
    };

    class GettersSetters {
        constructor() {
            return new Proxy(this, withGettersSetters);
        }
    }

    class getSetGen extends GettersSetters {
        constructor([obj , ...params]) {
            super();
            params = [...params]
            var that = this;
            var objectKeys = Object.keys(obj);
            var param = params.map(x => x)
            objectKeys.forEach((el, index) => {
                that.el === param[index]



            });
        }
    }
    const GetSetGen = new getSetGen([_object , 'SD-v12e', 'Street sw2', 2020]);

    console.log(GetSetGen.getId()); // undefined
Enter fullscreen mode Exit fullscreen mode

the main proplem it here objectKeys.forEach((el, index) => {
that.el =param[index]});

i think all i need ti do here's i want to loop throw object propeties and use it and as u see from my params i send the values and loop throw it ans evently i need to
to make equality between them like here
that.el = param[index]
but i all got undefined
console.log(GetSetGen.getId()); // undefined