Details

var_dump for AS3

Date
2008-09-12
Context
Downloads / Flash
Link
http://snipplr.com/view/8195/
Topics
/
Comments
7 comments


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()

7 Responses to “var_dump for AS3”

  1. Name
    Alex
    Date
    November 20th, 2008
    Time
    01:41

    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

  2. Name
    Benji
    Date
    November 20th, 2008
    Time
    08:05

    @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.

  3. Name
    Benji
    Date
    November 20th, 2008
    Time
    09:32

    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

  4. Name
    MajiD Fatemian
    Date
    February 1st, 2009
    Time
    01:31

    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.

  5. Name
    Sebastian Paaske Tørholm
    Date
    June 21st, 2009
    Time
    23:31

    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()

  6. Name
    Sebastian Paaske Tørholm
    Date
    June 21st, 2009
    Time
    23:32

    Remove the “name + “: ” from that, actually.

  7. Name
    Benji
    Date
    June 22nd, 2009
    Time
    08:19

    Thanks for this upgrade Sebastian. I’ll include it in the Snipplr.com snippet.

Leave a Reply

Acknowledgement

© 2012 by Benjamin Wiederkehr. Benjamin Wiederkehr's Blog runs smoothly with Wordpress 3.3.1 on a NovaCompany Server.