10

这个问题是故意用这个问题来表述

我什至不知道这是否可能,我记得隐约听到一些关于 JS 中无法枚举的属性。

无论如何,长话短说:我正在一个 js 框架上开发一些东西,我没有文档也无法轻松访问代码,这将极大地帮助了解我可以用我的对象做什么。

4

4 回答 4

16

如果您在项目中包含Underscore.js,则可以使用_.functions(yourObject).

于 2013-09-03T23:44:23.473 回答
11

我认为这就是你要找的:

var obj = { locaMethod: function() { alert("hello"); }, a: "b", c: 2 };
for(var p in obj)
{
    if(typeof obj[p] === "function") {
      // its a function if you get here
    }
}
于 2010-12-04T16:36:45.247 回答
3

您应该能够枚举直接在对象上设置的方法,例如:

var obj = { locaMethod: function() { alert("hello"); } };

但大多数方法都属于对象的原型,如下所示:

var Obj = function ObjClass() {};
Obj.prototype.inheritedMethod = function() { alert("hello"); };
var obj = new Obj();

因此,在这种情况下,您可以通过枚举 Obj.prototype 的属性来发现继承的方法。

于 2010-12-04T10:13:09.180 回答
1

您可以使用以下内容:

var obj = { locaMethod: function() { alert("hello"); }, a: "b", c: 2 };


for(var p in obj)
{
    console.log(p + ": " + obj[p]); //if you have installed Firebug.
}
于 2010-12-04T10:19:58.413 回答