如何获取对象的方法?

发布于 2024-11-04 14:39:00 字数 228 浏览 0 评论 0原文

是否有方法或属性可以从对象中获取所有方法?例如:

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

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

UPDATE: Jquery 中有类似的方法吗?

谢谢。

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() {}

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

UPDATE: Are there any method like that in Jquery?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

莫相离 2024-11-11 14:39:01
var methods = [];
for (var key in foo.prototype) {
    if (typeof foo.prototype[key] === "function") {
         methods.push(key);
    }
}

您可以简单地循环构造函数的原型并提取所有方法。

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.

碍人泪离人颜 2024-11-11 14:39:01

最好的方法是:

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

仅在 es6 中使用 'let',而使用 'var' 代替

the best way is:

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

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

弃爱 2024-11-11 14:39:01

在 Chrome 中是 keys(foo.prototype)。返回[“a”,“b”]

请参阅:https://developer.chrome.com/devtools/docs/commandline- api#keysobject在此处输入图像描述

稍后编辑:如果您需要快速复制它(对于更大的对象),请执行 copy(keys(foo.prototype)) 你就会把它放在剪贴板中。

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.

小嗲 2024-11-11 14:39:01

获取方法名称:

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

或者,获取方法:

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

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];
    });
};
一百个冬季 2024-11-11 14:39:00
function getMethods(obj)
{
    var res = [];
    for(var m in obj) {
        if(typeof obj[m] == "function") {
            res.push(m)
        }
    }
    return res;
}
function getMethods(obj)
{
    var res = [];
    for(var m in obj) {
        if(typeof obj[m] == "function") {
            res.push(m)
        }
    }
    return res;
}
救赎№ 2024-11-11 14:39:00

请记住,从技术上讲,JavaScript 对象没有方法。它们具有属性,其中一些可能是函数对象。这意味着您可以枚举对象中的方法,就像枚举属性一样。这个(或类似的东西)应该可以工作:

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

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

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.

一场春暖 2024-11-11 14:39:00

您可以使用console.dir(object) 将该对象属性写入控制台。

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

失退 2024-11-11 14:39:00

在现代浏览器中,您可以使用 Object.getOwnPropertyNames获取对象上的所有属性(可枚举和不可枚举)。例如:

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

请注意,这仅检索自己的属性,因此它不会返回在原型链上其他地方找到的属性。然而,这似乎不是您的要求,所以我认为这种方法就足够了。

如果您只想查看可枚举属性,则可以使用Object.keys。这将返回相同的集合,减去不可枚举的构造函数属性。

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.

时光与爱终年不遇 2024-11-11 14:39:00

对我来说,获取最终扩展类的方法的唯一可靠方法是这样做:

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

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
}
摇划花蜜的午后 2024-11-11 14:39:00

可以使用浏览器的开发人员工具 (F12) 在对象的原型链中检查这些方法:

  console.log(yourJSObject);

或更直接

  console.dir(yourJSObject.__proto__);

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__);
清君侧 2024-11-11 14:39:00

在 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"]

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"]
七分※倦醒 2024-11-11 14:39:00
var funcs = []
for(var name in myObject) {
    if(typeof myObject[name] === 'function') {
        funcs.push(name)
    }
}

我使用的电话没有分号:),但这是一般的想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文