Sunday, August 3, 2008

Javascript Object generic toString method to display object Content.

It has been around 2 weeks me rigorously working on javascript.Well working on javascript is no big deal if you already know java and have a will to learn new although sometimes very frustrating because debugging is a pain in the arse(although firebug tool really comes handy here must tool for the web developer i guess) and sometimes you really feel that yr browser has gone mad,but i really like the fact that the development time is very less as you can easily see the result just use the alert and here you are with anything you want and there is no need to compile or anything.

Writing jsUnit test cases for javascript makes thing somewhat simpler,but an extra coding effort kills me but  is necessary if you are working in a corporate life :) ,And in think the age of web 2.0 the need of well defined javascript structure is a must although so many open source JS libraries already available on net writing good structured scripts in js is not at all difficult these days.

Well i have been working on YUI these days i must say that Yahoo has done a great effort in making the library and also has a big heart in bringing the library open to the world to use with lots of tutorial available.The YUI event library specially is beyond comparison,another library which i like is DOJO it makes many things very simple if you are concentrating on the UI but the library is too heavy.Also heard about many other libraries like ext js , gwt, prototype but didn't get time to lay hands on them.

Well currently working on project i came with problem of how to display the data within the js objects which i require sometimes  for debugging purpose and js doesn't give a built in  toString() method for the Objects like it provides for the array.The data inside the objects is usually in the form of key and value pairs where value can be again any type of object so the object structure varies from object to object.

So i started writing separate display functions for displaying the data inside the object in simple terms the toString method for the objects separately.After some time i got frustrated as the number of objects increased and i didn't want to do the monotonous work again and again.So i thought of writing a generic js object(just like a class in JAVA) which has a generic toString function and what ever be the structure of the object it just display the content of the object.

well after spending 1 to 2 hour i came out with a generic js object to display the content of the object although the formatting of the display text was not so good but it solved my purpose(i am still working on the formatting part),now i guess the life would be more simpler for me :).

i have added the js file to download,Hope it helps you.In case of bugs do tell me(i didn't tried lot of objects it worked with mine object types). 



Download : Js script available here

You can use the printObject.js by including this script to your own script by using the tag inside the head tag

Or you can directly copy the object to your own js file.

after that you can create a new object of the printObject using new operator by using
var test = new PrintObject(object to display);

and then
test.toString();
function to display your object.
to change the object to a new object use the function
test.changeObject(new object);