Like a lot of people, I use the trace function as my main debugger. It’s great for tracing out numbers or strings, or even arrays of numbers or strings. But when you get into objects, you wind up with something like [object Object], which isn’t too enlightening.
OK, so then we get in to for-in loops, and doing something like this:
for(var prop in myObj){ trace(prop + ": " + myObj[prop]); }
Again, very useful, but I get sick of typing all that every time. And say one of the properties of myObj is itself and object? You wind up with for-in loops within for-in loops, tedious to type, and you wind up deleting it anyway once you’ve found your bug, or determined it’s not in that object.
So, I made a little recursive function for myself. I have a utils class in my class path, and just added this static method to it:
public static function traceObject(o:Object, recurseDepth:Number, indent:Number):Void { if(recurseDepth == undefined){ var recurseDepth:Number = 0; } if(indent == undefined){ var indent:Number = 0; } for(var prop in o){ var lead:String = ""; for(var i=0;i 0){ traceObject(o[prop], recurseDepth-1, indent+1); } } }
Now for any object, myObj, I can just say:
utils.traceObject(myObj);
and I get a nicely formatted output of every property in the object. If I want to recursively trace out the objects two levels down:
utils.traceObject(myObj, 2);
Just be careful about recursing too deeply if your object has references to components, or if you are dealing with a large array of complex objects. I have managed to crash Flash by doing so. 🙂
Thanks for this function man, I wanted to write one like this but I never did. Thanks again
Buddha
Thanks, that’s a nice drop-in!
nice…i’m curious to check out what else is in that utils class of yours.
Seems like everyone’s written one of these …
Macromedia might as well add it to next actionscript rev!
Never said it was original, just cool! 🙂
My utils class has a few xml functions, a few string functions and a timer function.
The one I use the most just strips leading and trailing white space from a string. Useful for cleaning up text nodes from formatted xml.
Handy :).
Mmm, TracerMonkey!!!
very handy 🙂
i was use many types to trace many parameteres and write out values in browser:
_global.out=function(){
(_url.substr(0,8)==”file:///”) ? trace(arguments.join(“, “)) : getURL(“javascript:alert(‘”+arguments.join(“, “)+”‘)”);
}
Very neat! Thanks for sharing it.
Since your on the subject of tracing/debugging, you might be interested in this AS1 class: http://www.flamingmongrel.net/projects/viewProject.asp?p=16
It gives you a window for tracing expressions at runtime using a custom global function iTrace(). It’s got some flaws I need to address, but it’s been a really useful little tool.
here’s one i wrote a while back ago in AS1. 🙂
_global.traceOBJ = function(obj,level){
if(level==undefined){
trace(“\n\n//-Start of object array()\n”);
}
for(var items in obj){
if((typeof obj[items])==”object”){
traceOBJ(obj[items],level+ ” : ” +items);
}else{
trace(level+” : “+ items + ” : ” +obj[items]);
}
}
}
Looks familiar. The cool thing about AS2 is you don’t have to copy that function into your fla, or even #include anything. Just call the function based on its class name. As long as it’s in your class path, Flash will find it.
I’ll be using this quite a bit. Thanks.