macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript
I have written a treeTrace-method, the functionality
is good the code looks not so good.
Ralf Bokelberg and i have
refactored the code into little peaces, mainly
this can be good practice, because a method-call is
often more easy to understand than on big method.
If you want to read an good code example for this technic,
read ralfs Prealoader-Class, it is fantastic to read.
The Problem with treeTrace was the complexity of the
allgorithm which was after refactoring cut into many small
Peaces. The Allgorithm is not easy, because it implements,
an multidemensional depths search with depths-control
without using recursive-method-calls. Therefore it would
be more easy to read it in one Method.
#include "treeTrace.as"
// Test
obj = new Object();
obj.a1 = 42;
obj.a2 = new Array();
obj.a2[0] = new LoadVars();
obj.a2[1] = new Sound();
obj.a3 = 'test';
obj.a4 = 'test';
obj.a5 = new Object();
obj.a6 = 'test';
obj.a7 = function () {};
obj.a8 = 'test';
obj.a9 = new Object();
obj.a9.b1 = new Object();
obj.a9.b2 = 'test';
obj.a10 = new Object();
obj.a10.b1 = new Object();
obj.a10.b1.c1 = new Object();
obj.a10.b1.c1.d1 = new Object();
obj.a11 = new Object();
obj.a11.b1 = 'test';
obj.a12 = 'test';
obj.a13 = obj; // backreference-test
obj.a14 = new Object();
obj.a14.b1 = obj.a14; // backreference-test
obj.a15 = new Object();
obj.a15.b1 = new Object();
obj.a15.b2 = new Object();
obj.a15.b3 = new Object();
treeTrace(obj, 'obj');
// Output:
/*
----------------------------------------------------
__[obj:[object refers #0] = [object Object]]
|__[a1:[number] = 42]
|__[a2:[array #1] = ,[object Object]]
| |__[0:[loadvars #2] = ]
| |__[1:[sound #3] = [object Object]]
|__[a3:[string] = test]
|__[a4:[string] = test]
|__[a5:[object #4] = [object Object]]
|__[a6:[string] = test]
|__[a7:[function #5] = [type Function]]
|__[a8:[string] = test]
|__[a9:[object #6] = [object Object]]
| |__[b1:[object #7] = [object Object]]
| |__[b2:[string] = test]
|__[a10:[object #8] = [object Object]]
| |__[b1:[object #9] = [object Object]]
| |__[c1:[object #10] = [object Object]]
| |__[d1:[object #11] = [object Object]]
|__[a11:[object #12] = [object Object]]
| |__[b1:[string] = test]
|__[a12:[string] = test]
|__[a13:[object refers #0] = [object Object]]
|__[a14:[object #13] = [object Object]]
| |__[b1:[object refers #13] = [object Object]]
|__[a15:[object #14] = [object Object]]
|__[b1:[object #15] = [object Object]]
|__[b2:[object #16] = [object Object]]
|__[b3:[object #17] = [object Object]]
----------------------------------------------------
*/
//treeTrace(_global, '_global', true);
Look at the result for this:
treeTrace(_global, '_global', true);
Get the as-File
Posted by hOk at May 8, 2003 08:48 PM
Comments (7)
"without using recursive-method-calls."
Why didn't you use recursive calls? It would make your function about 5-10 lines long.
Just curious.
Posted by Peldi at May 8, 2003 10:33 PM
Yes, youre right and the code would
be much more readable, but i have to
trace sometimes realy big structures
and than the recursion failes because
it is limited to 256 steps.
Posted by hOk at May 8, 2003 11:58 PM
Structures of depth > 256 ? :)
But anyways, i think it is a nice example of making a recursive function non-recursive
Have a nice day
bokel
Posted by bokel at May 9, 2003 10:39 AM
Oh man, i am so stupid...;-)
I thought that this would not
work:
traceProperties = function (pObj) {
for (var p in pObj) {
if (typeof(pObj[p]) == 'object') {
arguments.callee(pObj[p]);
}
trace(p);
}
}
a = {name:'a'};
for (var i = 0; i
...because the function calls more than
256 times it self, but that is no problem
because the depths stays at level 0,
thank you Peldi and bokel for bringing
me back to the right way, Holger
Posted by hOk at May 9, 2003 02:53 PM
Well, here is the
recursive-Version some other,
small improvements and with one
big improvement - it is readable...;-)
regards, Holger
Posted by hOk at May 9, 2003 10:30 PM
Dear Holger,
I've also created similar tracing utility. But mine traces it as actionscript so that we can use the output to rebuild the object if we need.
#include "toString.as"
trace(ActionScript(obj,'obj'));
produces the following output for your object
Regards,
Arul
obj = {
a15:{
b3:{},
b2:{},
b1:{}
},
a14:{},
a12:"test",
a11:{
b1:"test"
},
a10:{
b1:{
c1:{
d1:{}
}
}
},
a9:{
b2:"test",
b1:{}
},
a8:"test",
a6:"test",
a5:{},
a4:"test",
a3:"test",
a2:[
{},
{}
],
a1:42
}
obj.a14.b1 = obj.a14;
obj.a13 = obj;
obj.a7 = function(){
}
Posted by Arul at November 10, 2003 02:03 AM
I forgot to give the link. Here it is
http://www.shockwave-india.com/blog/actionscript/?asfile=toString.as
Regards,
Arul
Posted by Arul at November 10, 2003 02:27 AM
|
You can use <code>code that should be highlightned</code> to highlight code! Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise actionscript-highlightning will be used. |
