How can I inspect an Object in an alert box? Normally alerting an Object just throws the nodename:
alert(document);
But I want to get the properties and methods of the object in the alert box. How can I achieve this functionality, if possible? Or are there any other suggestions?
Particularly, I am seeking a solution for a production environment where console.log and Firebug are not available.
Here is my object inspector that is more readable. Because the code takes to long to write down here you can download it at http://etto-aa-js.googlecode.com/svn/trunk/inspector.js
Use like this :
This is blatant rip-off of Christian's excellent answer. I've just made it a bit more readable:
How about
alert(JSON.stringify(object))
with a modern browser?In case of
TypeError: Converting circular structure to JSON
, here are more options: How to serialize DOM node to JSON even if there are circular references?The documentation:
JSON.stringify()
provides info on formatting or prettifying the output.There are few methods :
In a console context, sometimes the .constructor or .prototype maybe useful:
The
for
-in
loops for each property in an object or array. You can use this property to get to the value as well as change it.Note: Private properties are not available for inspection, unless you use a "spy"; basically, you override the object and write some code which does a for-in loop inside the object's context.
For in looks like:
Some sample code:
Edit: Some time ago, I wrote my own inspector, if you're interested, I'm happy to share.
Edit 2: Well, I wrote one up anyway.