samedi 17 juillet 2021

Why am I getting undefined even using bind?

    interface LooseObject {
        [key: string]: any
    }
    
    function f(obj: object)
    {
        let p:LooseObject = {};
        for (; obj != null; obj = Object.getPrototypeOf(obj))
        {
            for(var [k, v] of Object.entries(Object.getOwnPropertyDescriptors(obj)))
            {
                if(typeof v.get === 'function' && k !== '__proto__')
                {
                    const vv = v.get.bind(obj);
                    console.log('key = ', k, ', value = ', vv())
                }
            }
        }
        return p;
    }

class Foo
{
    #x;
    #y;

    constructor()
    {
        this.#x = 20;
        this.#y = 10;
    }

     get a()  { return "baa"; }
     get n()  { return 20; }
}

class B extends Foo
{ 
    get z(): number { return 30; }
}

then

let o = new B()
f(o)

but I'm getting undefined from this line const vv = v.get.bind(obj); what I missing?





Aucun commentaire:

Enregistrer un commentaire