Theia installation has been updated - September 10, 2011
« May 2012
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Theia
Dealing with a lack of print_r in JavaScript « home
icon
posted on 15:59 - 10 November 2010 | posted by Lev
last modified on 15:59 - 10 November 2010 | last modified by Lev
I see an awful lot of threads and articles while googling for a good equivalent to PHP's print_r() function for use in JavaScript - since you frequently need to dump an array or object for debugging purposes.
 
Instead of bundling in one of the countless (and limited) functions I've come across, the best approach I've seen at doing just this involves using Firebug's console. Let's face it: if you are a web developer: you are using Firebug already (if you aren't: what the hell is wrong with you?!).
 
It's incredibly simple. Let's say you want to dump the current object in a way that lets you see all its properties...
 
JAVASCRIPT CODE
console.log(this);
 
Simple eh?
 
Obviously, you'll want to substitute this with whatever the name of your array or object is, and the console pane in Firebug will show you what you are interested in. I am ashamed to say I just learned this today - this would have saved me loads of time if I knew it earlier. >_<
tags
post reply
untitled
quote
posted by: Lev · date: 22:05 - 20 November 2010
lev
Yeah, acido - you're completely correct - I hadn't even taken that into consideration.
 
Sometimes I just assume everyone is using Firebug.. >_<
 
Good idea if you want to make your code portable and plan to distribute any debugging info with your javascript.
 
Do you happen to know if IE has any native solution you could use for when IE is being used?
"Be the change you want to see in the world." - Gandhi
apprentice » 6863 AP » total items: 554 / total posts: 26 » 0.143 / 1.143
use a wrapper
quote
posted by: guest 83.254.188.124 · date: 19:00 - 19 November 2010
A good thing to do when you are using console.log is to make a wrapper function to make sure that javascript don't crash. Something like this:
 
function trace(obj){
     if(instanceof(console) != 'undefined' && console != null)
         console.log(obj);
}
 
 
otherwise all of the javascript will crash on the site if firebug or the debug tools in chrome isn't active.
 
 
//acido
Bookmark item @
bookmarkbookmarkbookmarkbookmarkbookmark