2

I need to discover the function 'foo' on class Something and it doesn't work. Is this because I am missing a subtlety of using class syntax or because of babel or something else? I would prefer to use 'class' wherever I can but the discovery is more important for me.

Any geniuses our there know this one?

class Something {
    constructor() {
    }
    foo() {
        console.log("foo");

    }

}

var something = {
    foo : function() {
        console.log("foo");
    }
}

var result = Object.getOwnPropertyNames(new Something()).join(", ");
console.log("new Something() = %s", result);

result = Object.getOwnPropertyNames(Something.__proto__).join(", ");
console.log("Something.__proto__ = %s", result);

result = Object.getOwnPropertyNames(something).join(", ");
console.log("var something = %s", result);

produces:

new Something() = 
Something.__proto__ = length, name, arguments, caller, constructor, bind, call, apply, toString
var something = foo
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76

1 Answers1

0

Ok, the above was a simplified question to what I was trying to achieve which was this:

module.exports.toDto = (object) =>{
    var result = {};
    Object.getOwnPropertyNames(object.__proto__).map(k => k.match("get(.*)")).filter(m => m).forEach(m => {
        var prop = m[1].toLowerCase();
        var value = object[m[0]]();
        result[prop] = value;
    });

    return result;
};

This allows me to discover properties on a class. Hurray!

Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • It is tricky here. object.__proto__ won't list everything. You should use object.prototype all instance method and object for static method. – Weijing Jay Lin Jun 04 '18 at 19:29