How to get an object's methods?

前端 未结 12 1142
暗喜
暗喜 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 03:45

    Get the Method Names:

    var getMethodNames = function (obj) {
        return (Object.getOwnPropertyNames(obj).filter(function (key) {
            return obj[key] && (typeof obj[key] === "function");
        }));
    };
    

    Or, Get the Methods:

    var getMethods     = function (obj) {
        return (Object.getOwnPropertyNames(obj).filter(function (key) {
            return obj[key] && (typeof obj[key] === "function");
        })).map(function (key) {
            return obj[key];
        });
    };
    

提交回复
热议问题