Jeff Meatball Yang Jeff Meatball Yang - 1 year ago 108
Javascript Question

In Chrome JS debugger, how do I print all the properties of an object?

I have opened the Javascript Debugger (Ctrl+Shift+L) in Chrome and started using it to set breakpoints in my code.

This is a much different interface compared to Firebug, (it's all command line driven) so I'm wondering how to do a simple thing like print all the properties of an object.

If I have an object like this:

var opts = {
prop1: "<some><string/></some>",
prop2: 2,
prop3: [1,2,3]

I can set a breakpoint and inspect the object, but I only seem to get a single property back, and I'm not sure which property will appear:

$ print opts
#<an Object>

Trying to get all the properties:

$ print for(var p in opts) p;

Any ideas?
It obviously has more than just one...

Answer Source

So I've tried using the "dir" command, and it gives me something at least:

$dir opts 
3 properties  
prop1: string (#11#) 
prop2: string (#12#)  
prop3: string (#13#)

This also works (slightly better because it gives me some values), but cuts off the end of the string if it's too long:

$ print var s=[];for(var p in opts) { s.push(p + ":" + opts[p]); } s.join(",");
prop1:<some><string/></some>,prop2:2,prop3:[object Object]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download