如何获取对象的方法?

是否有一个方法或属性从一个对象获取所有方法? 例如:

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


foo.get_methods(); // returns ['a', 'b'];

更新: Jquery 中有类似的方法吗?

谢谢你。

123645 次浏览
function getMethods(obj)
{
var res = [];
for(var m in obj) {
if(typeof obj[m] == "function") {
res.push(m)
}
}
return res;
}
var funcs = []
for(var name in myObject) {
if(typeof myObject[name] === 'function') {
funcs.push(name)
}
}

I'm on a phone with no semi colons :) but that is the general idea.

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);
}

这很复杂,因为对象的某些属性是不可枚举的,所以您将无法在对象上找到every函数。

var methods = [];
for (var key in foo.prototype) {
if (typeof foo.prototype[key] === "function") {
methods.push(key);
}
}

You can simply loop over the prototype of a constructor and extract all methods.

In modern browsers you can use Object.getOwnPropertyNames to get all properties (both enumerable and non-enumerable) on an object. For instance:

function Person ( age, name ) {
this.age = age;
this.name = name;
}


Person.prototype.greet = function () {
return "My name is " + this.name;
};


Person.prototype.age = function () {
this.age = this.age + 1;
};


// ["constructor", "greet", "age"]
Object.getOwnPropertyNames( Person.prototype );

Note that this only retrieves own-properties, so it will not return properties found elsewhere on the prototype chain. That, however, doesn't appear to be your request so I will assume this approach is sufficient.

If you would only like to see enumerable properties, you can instead use Object.keys. This would return the same collection, minus the non-enumerable constructor property.

You can use console.dir(object) to write that objects properties to the console.

In Chrome is keys(foo.prototype). Returns ["a", "b"].

See: https://developer.chrome.com/devtools/docs/commandline-api#keysobjectenter image description here

Later edit: If you need to copy it quick (for bigger objects), do copy(keys(foo.prototype)) and you will have it in the clipboard.

In ES6:

let myObj   = {myFn : function() {}, tamato: true};
let allKeys = Object.keys(myObj);
let fnKeys  = allKeys.filter(key => typeof myObj[key] == 'function');
console.log(fnKeys);
// output: ["myFn"]

the best way is:

let methods = Object.getOwnPropertyNames(yourobject);
console.log(methods)

use 'let' only in es6, use 'var' instead

The methods can be inspected in the prototype chain of the object using the browser's developer tools (F12):

  console.log(yourJSObject);

or more directly

  console.dir(yourJSObject.__proto__);

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];
});
};

for me, the only reliable way to get the methods of the final extending class, was to do like this:

function getMethodsOf(obj){
const methods = {}
Object.getOwnPropertyNames( Object.getPrototypeOf(obj) ).forEach(methodName => {
methods[methodName] = obj[methodName]
})
return methods
}