Comparable print_r function in as3

In php, we have a print_r () method.

Is there anything like this in as3 for printing an object / array?

+2


a source to share


3 answers


I found this :



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

      

+2


a source


Check [ ObjectUtil.toString

] ( http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#toString ()) .



0


a source


Here's another example:

    public function rTrace(obj:*, tab:Number=0):void {
        if(tab > 10) return;
        for(var key:String in obj) {
            if(typeof(obj[key]) == "object" || obj[key] is Array) {
                tr(doTab(tab)+key+"["+typeof(obj[key])+"]= {");
                rTrace(obj[key], tab+2);
                tr(doTab(tab)+"}");
            } else
                tr(doTab(tab)+key+"["+typeof(obj[key])+"]="+obj[key]);                  
        }
    }
    private function doTab(n:Number):String {
        var out:String = "";
        for(var i:Number=0; i<n; i++) 
            out += " ";
        return out;         
    }       
    private function tr(msg:String):void {
        trace(msg);
    }

      

Usage example:

var test:Object = {id:"asdf", count:17, 
                   childs:[1,2,7,39], tasks:{task_1:"test", task_2:true}};
rTrace(test);

      

Produces output:

tasks[object]= {
  task_2[boolean]=true
  task_1[string]=test
}
count[number]=17
childs[object]= {
  0[number]=1
  1[number]=2
  2[number]=7
  3[number]=39
}
id[string]=asdf

      

0


a source







All Articles