1

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?

5
  • what do you mean unknown ? can you create an instance of it ? Commented Mar 2, 2010 at 19:35
  • Why can't you use JSON? If you can see the object, you can use JSON. Commented Mar 2, 2010 at 19:35
  • I tried using JSON and it didn't work. Potentially I did something wrong. Commented Mar 2, 2010 at 19:49
  • Don´t you get a nice object tree in firebug if you console.log(myObject);? Commented Mar 2, 2010 at 20:32
  • No, I get a reference error because the object is an ActiveX control and doesn't work in Firefox. :( Commented Mar 2, 2010 at 20:48

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

for debugging you could also use Firebug extension on Firefox, there you can simply dump an object and see what methods/properties it has...
Debugging is what I'm trying to do. However this object is an ActiveX object that only works in IE...so is there an equivalent action in IE's Web Developer Tools?
there is also Firebug Lite, 100% JavaScript solution, but for this you need to be able to include somehow JS file. See their page for more details: getfirebug.com/firebuglite
0

If you have VS 2005 have a look at Debugging client JavaScript in VS 2005

Comments

0

you can use function_name.prototype.constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.