与as3中的print_r相当的功能

时间:2010-04-22 19:32:57

标签: actionscript-3

在php中我们有方法print_r()。

as3中是否有类似的内容可以打印出一个对象/数组?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

答案 2 :(得分:0)

这是另一个例子:

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

使用示例:

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

给出输出:

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