开发者

Get definition of unknown object in Javascript

开发者 https://www.devze.com 2022-12-22 04:00 出处:网络
Is there any way to get the definition (public methods and properties) of an object in开发者_如何学运维 Javascript that is unknown?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消