Hey there – this is my first post here, this seems like the place to post javascripts.
I’m not sure whether I’ve gone out on a limb to reinvent the wheel, but I’ve produced a data dumper, very similar to the one in Perl ([URL=http://search.cpan.org/author/ILYAM/Data-Dumper-2.121/Dumper.pm]here
The main function that the user should be interested in is a prototype of Object called dump. It can be fed an optional associative array of options (examples below). The method recursively looks through the object and returns the content of the object (excluding functions)
I’ve attached the script (as a txt file), but I’ve uploaded it to my site as well, so you can use the example below – replace testArray with any object/string/array/number you like
[code=php]
<SCRIPT LANGUAGE=”JavaScript” src=”http://www.petercracknell.com/include/js/dump.js”></SCRIPT>
<SCRIPT LANGUAGE=”JavaScript” src=”dump.js”></SCRIPT>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
function myFancyObject(asd, sdf, sdf) {
this.xyzxyz = {
longword: 4,
c: [3,5,6],
d: { w: [3,5] }
};
this.abc = [1, 2, 3]
}
var testArray = new Array(new myFancyObject(), 4, [4,5]);
document.write(‘<pre>’);
document.writeln(testArray.dump());
document.writeln(testArray.dump({space: ‘_’}));
document.writeln(testArray.dump({object_detail: false}));
document.writeln(testArray.dump({wide_spacing: false}));
document.writeln(testArray.dump({quote_string: ““”}));
document.write(‘</pre>’);
</SCRIPT>
The script also contains Object.duplicate, Object.isArray, Object.isNumber, Object.isString, which I think are all relatively self-explanatory.
I’m interested in peoples feedback – I consider these Object functions finished, can anyone see any useful additions? Can anyone find any bugs, problems?
Finally has this been done before/bettered? I did search google, but couldn’t find one.
Pete
[upl-file uuid=fb1ba81a-fd2d-4869-937c-fe26463ab8a9 size=7kB]dump.txt