You quickly learn in Actionscript 3 that tracing an objects’ contents is not always that simple. Commands such as trace(myObject); often yeild the highly infuriating [object Object] return, which tells you mostly nothing of what you actually wanted to know. After that you can move on to more advanced trace logic, like the handy;
for(var i in n) {
trace(i+":"+n[i]);
}But that’s a lot of stuff to type, and often yeilds infuriatingly long stacks of parameter traces.
Flex can make things a little easier, with this useful utility class, the cunningly named ObjectUtil class. Simply import the class (import mx.utils.ObjectUtil;) and call the following method; trace(ObjectUtil.toString(myObject)); and you’ll get a nice output of all the properties of your object.
Kudos to flextutor.org for this, in their original post “How to print an objects’ contents in Flex.” I thought I’d repost it here though, not least because there seems to be an issue loading their site as I’ m writing this article.
This is not true for all object. For instance the NetStream info object cannot be traversed in this way. Instead you would need to use describeType();
package {
import flash.display.Sprite;
import flash.utils.describeType;
public class DescribeTypeExample extends Sprite
{
public function DescribeTypeExample()
{
var child:Sprite = new Sprite();
var description:XML = describeType(child);
trace(description..accessor.@name.toXMLString());
}
}
}
As described here: http://livedocs.adobe.com/flex/2/langref/flash/utils/package.html#describeType()
Thanks, needed help printing the contents of my Object (associative array) and this did the trick. And, the other blog where this appeared still does not load, so thanks for the “re-post”
Very usefull information. Saved me alot of time so thank you!
Fantastic! Thank you! Just what I’ve been looking for!
Life saver, thank you very very much