How to list the functions/methods of a javascript object? (Is it even possible?)
This question is intentionally phrased like this question.
I don't even know if this is possible, I remember vaguely hearing something about some properties not enumerable in JS.
Anyway, to cut a long story short: I'm developing something on a js framework for which I have no documentation and no easy access to the code, and it would greatly help to know what I can do with my objects.
我认为这是你正在寻找的东西:
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
}
}
如果在项目中包含_.functions(yourObject)
,则可以使用_.functions(yourObject)
。
You should be able to enumerate methods that are set directly on an object, eg:
var obj = { locaMethod: function() { alert("hello"); } };
But most methods will belong to the object's prototype, like so:
var Obj = function ObjClass() {};
Obj.prototype.inheritedMethod = function() { alert("hello"); };
var obj = new Obj();
So in that case you could discover the inherited methods by enumerating the properties of Obj.prototype.
链接地址: http://www.djcxy.com/p/27372.html上一篇: javascript循环和删除对象属性