How to get an object's methods?

前端 未结 12 1145
暗喜
暗喜 2020-12-13 03:33

Is there a method or propertie to get all methods from an object? For example:

function foo() {}
foo.prototype.a = function() {}
foo.prototype.b = function()         


        
12条回答
  •  抹茶落季
    2020-12-13 04:00

    Remember that technically javascript objects don't have methods. They have properties, some of which may be function objects. That means that you can enumerate the methods in an object just like you can enumerate the properties. This (or something close to this) should work:

    var bar
    for (bar in foo)
    {
        console.log("Foo has property " + bar);
    }
    

    There are complications to this because some properties of objects aren't enumerable so you won't be able to find every function on the object.

提交回复
热议问题