Although Flex Builder and Eclipse do have nice debug utilities built in, today I needed a function/method in Actionscript to display into the current application some variables’ content. So I started to implement a var_dump alike function (var_dump from PHP language).
So I started and I made the Debug class that will implement the static dump function. Looking at the PHP var_dump function I noticed two, three main problems that will appear:
- data types - ex: array is displayed as object by typeof function/operator
- length of objects and associative arrays - ex: length property for objects and associative arrays is undefined
- behaviour of static methods and variables inside calsses - ex: a static variable will keep its content even new methods calls appear, so calling the static dump method will not reinitialize the static vars.
For al these problems solutions are available:
- instead of using typeof we will use flash.utils.getQualifiedClassName static method (see also this old article) - more about typeof operator and its return values here
- we will implement a method to count the first children in an object
- there will be an internal recursiveDump function and a generic public dump function
Here is the application code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="400" creationComplete="start()"> <mx:TextArea id="debugTA" x="10" y="10" width="480" height="380"/> <mx:Script> <![CDATA[ import mx.controls.Alert; import com.flexer.Debug; private var testdata:Object; private function start():void { // even though the object is created like here // the order does not reflect the order of // creation - the order is somehow made internally testdata = new Object(); testdata["row1"] = { "col1":"a", "col2":{ "12":"12", "13":[1,2,3,4] }, "col3":"c", "col4":"d" }; testdata["row2"] = new XML(); testdata["row2"] = <test><tag1 att="att">val</tag1></test>; testdata["row3"] = {"col1":"a","col2":"b","col3":"c","col4":"d"}; testdata["row4"] = {"col1":"a","col2":"b","col3":"c","col4":"d"}; testdata["row5"] = new Array(); testdata["row5"] = ["a","b","c","d"]; testdata["row6"] = {"col1":"a","col2":"b","col3":"c","col4":"d"}; // calling debug.dump debugTA.text = Debug.dump(testdata,3,5.5); } ]]> </mx:Script> </mx:Application> |
The Debug class with its static methods can be downloaded at the end but now you can see the dump of the above flex code.
Popularity: 60%
| ||
|
Tags: ActionScript, class, Debug, static
This post was written by Andrei Ionescu
Views: 1274



















nice try,
you can also do ObjectUtil.toString(testdata), it will give you same result with regular lib.
…but these are not major drawbacks so use ObjectUtil.toString with confidence.