
The var_dump for AS3 is a code snipped i use on a regularly basis to get information about an object when developping with AS3 in Adobe Flash. It contains two functions: analyze() and write(). The first is a recursive function that analyzes a given object. It uses the second function to trace out the gathered infromation. If you want to output the information elsewhere you can simply customize the write() function.
The code snippet is hosted on snipplr.com and I would be glad to receive feedback and suggestion on how to improve the code.
public function analyze(_obj):void {
var item:Object;
switch (typeof(_obj)){
case "object":
write("<object>");
write(_obj.toString());
for each (item in _obj){
analyze(item);
};
write("</object>");
break;
case "xml":
write("<xml>");
write(_obj);
write("</xml>");
break;
default:
write(_obj + " (" + typeof(_obj) + ")");
break;
};
} // analyze()
public function write(_obj):void{
trace(_obj);
} // END write()
var item:Object;
switch (typeof(_obj)){
case "object":
write("<object>");
write(_obj.toString());
for each (item in _obj){
analyze(item);
};
write("</object>");
break;
case "xml":
write("<xml>");
write(_obj);
write("</xml>");
break;
default:
write(_obj + " (" + typeof(_obj) + ")");
break;
};
} // analyze()
public function write(_obj):void{
trace(_obj);
} // END write()

Hi there,
This function doesn’t seem to be printing object variables. I’ve an object called Slide with internal strings and numbers stored within. When I used your analyze function it simply just prints;
[object Slide]
Any ideas why? Logically it should work, the for each() doesn’t seem to be doing its job.
Cheers
@Alex: Thanks for pointing out this issue. I’ll have a look at it to see where the problem lies and get back at you as soon as possible.
Update: the typeof() function returns ‘object’ for an array. so there’s no need to specifically include it in the switch-case query.
@Alex: These examples work correctly in my demo:
var boolean:Boolean = false;
var string:String = “Lorem”;
var number:Number = 23;
var array:Array = new Array(1,2,”3″);
var object:Object = new Object();
object.string = string;
object.number = number;
object.array = array;
analyze(boolean);
analyze(string);
analyze(number);
analyze(array);
analyze(object);
If you still got problems with the updated version of the function please submit the missbehaving object and I’ll try to fix it.
Benjamin
Hey benjamin
Great Job, It’s really helpful.
But as Alex raised it doesn’t work for objects.
It works fine for an object like the one you mentioned in your example, but consider an object which comes from a custom class.
var myObj:myCustomClass = new myCustomClass();
analyze(myObj);
it won’t work.
If you can update it it will be a great relief for all AS developers.
I modified it a bit to include names of fields dumped in objects.
function analyze(_obj):void {
var item:Object;
switch (typeof(_obj)){
case “object”:
write(“”);
for (var key in _obj){
write(“”);
analyze(_obj[key]);
write(“”);
};
write(“”);
break;
case “xml”:
write(“”);
write(_obj);
write(“”);
break;
default:
write(name + “: ” + _obj + ” (” + typeof(_obj) + “)”);
break;
};
} // analyze()
Remove the “name + “: ” from that, actually.
Thanks for this upgrade Sebastian. I’ll include it in the Snipplr.com snippet.