开发者

comparable function to print_r in as3

开发者 https://www.devze.com 2022-12-27 16:36 出处:网络
In php we have the method print_r(). Is there anything similar in as3 to print out an object/ar开发者_高级运维ray?I found this:

In php we have the method print_r().

Is there anything similar in as3 to print out an object/ar开发者_高级运维ray?


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


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


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);
    }

Example usage:

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

Gives the 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

精彩评论

暂无评论...
验证码 换一张
取 消