views:

314

answers:

3

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;
prop1

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

A: 

Try using the command line in the JavaScript console at the bottom of the Inspector (Ctrl+Shift+J). It has a much more Firebug-like feel to it.

Alex Barrett
How do I set a breakpoint using the Inspector? My problem is that I want to pause execution and inspect the value of the object.
Jeff Meatball Yang
+2  A: 

Chrome has ECMA-style native JSON, so you can use

JSON.stringify (opts);
{"prop1":"<some><string/></some>","prop2":2,"prop3":[1,2,3]}
Javier
This is good to know, but it seems like it doesn't work in the debugger. (ReferenceError: JSON is not defined)
Jeff Meatball Yang
A: 

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]
Jeff Meatball Yang