Get definition of unknown object in Javascript
Is there any way to get the definition (public methods and properties) of an object in开发者_如何学运维 Javascript that is unknown?
In reality, I know what the object should be, but it's having problems accessing Methods that should be there, so I want to see what Methods are defined.
I have no control over this object so I can't use JSON or toString. Any other ideas?
Check out "Javascript: The Good Parts", page 23 on reflection.
Some notes: use for(key in o) to enumerate the members of o. This will include members inherited via the prototype chain.
You can use o.hasOwnProperty(name) to determine if something is a direct member of an object, or included via the protoype chain.
You can use typeof() to distinguish functions from properties.
If you already expecting a known type of object, but still rather would like to check either particular method exists, you could do this with:
if ("method" in object) ....
If you have VS 2005 have a look at Debugging client JavaScript in VS 2005
you can use function_name.prototype.constructor
精彩评论