Click here to Skip to main content
15,886,769 members
Articles / Programming Languages / Javascript

Simple Object Inspector/Visualizer for JavaScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Sep 2013CPOL3 min read 15.2K   8   6
A very simple and most efficient object visualizer for JavaScript

Introduction

In JavaScript, everything is an object. But most of the times, nobody cares to give full information on the attributes of any of those objects, thinking 'it must be obvious'. When it is not, this simple utility function will visualize, in a concise and efficient way, the contents of a given object.

Background

When you try to use the code of JavaScript libraries, one of the biggest headaches is the uncertainty of the documentation. Open source is nothing without proper explanations. Very often, you are left wondering about the attributes of an obscure variable or parameter that floats around in the code. It seems that most of the authors think you should know what they thought. The best way to start to understand what is going on and how you can work in your code is to inspect the obscure object(s) instead of searching lousy documentation. This simple utility function will just return a string, formatted in the most efficient way, explaining what the attributes of a given object are and what values are present in them. Very simple and useful. There are some such functions around, but this one does not inflate the return string with lots of unnecessary characters, especially those got from the typeof() JavaScript function that is too verbose. The types of the attributes are converted into single characters that take the shortest space and are still informative. Also, there is an option for the separator string so that you can format the result as you like, for an alert() call or a log string.

Using the Code

The code is very easy to use, just put it inside a <script> tag on your HTML page and call it from any of your functions passing as parameters the object to inspect, the separator string to be inserted between attributes and an optional start string:

JavaScript
function ObjInspect(Obj, sSeparator, sText)
{    
    if (typeof sText == 'undefined') sText = '';
    if (typeof sSeparator == 'undefined') sSeparator = ',';
    
    if (sText.length > 64) return '[MAX LEN!]';
    var r = [];
    for(var p in Obj)
    {
          var t = typeof Obj[p];

        if (t == 'number') t = 'n';
        else if (t == 'string') t = 's';
        else if (t == 'boolean') t = 'b';
        else if (t == 'function') t = 'fnct';
        else if (t == 'null') t = 'N';
        else if (t == 'undefined') t = 'undef';
            
        r.push(sText + p + '[' + t + ']=' + (t == 'object' ? 'obj:' + 
          ObjInspect(Obj[p], sText + ';') : Obj[p]));
        }
    return r.join(sText + sSeparator);
}

The separator string defaults to a comma (","), the best option to generate strings to be used in logging operations. If you use the output for an alert(), pass "\n" to format the string with a line for each attribute. The types of the attributes are shortened to single characters with these values:

  • n for numerical
  • s for string
  • b for boolean
  • N for NULL
  • fnct for function
  • undef for undefined

In the sample code, there is a simple HTML page that shows the usage of this function to display an alert() message to the user. You can access the sample page at http://www.ultramundum.org/obj_inspect.html.

Points of Interest

The resulting strings from other similar functions were bloating my logs, so I shortened the information to the maximum. The result may be more difficult to read and may not cover all situations, but it is efficient and during the debugging of JavaScript, this is important. Also, the type characters are reminiscent of the Hungarian notation method, that I find most efficient and informative. I know you may will not like the formatting of the code, but I think it is more readable in this way. :-)

History

  • Rel. 1.0
This article was originally posted at http://www.ultramundum.org/obj_inspect.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President Ultramundum foundation
Italy Italy
Fulvio Dominci Carnino was born in Torino, Italy, in 1965.
One of the founders of the first italian computer club, he studied electronics in the Turin university. Before graduating he wrote his first videogame, Specventure.
The title sold more than 15000 copies all over Europe, a success for 1985, and madeDominici the first italian ever to sell a videogame on the international market.
In 1986 creates a real-time multitasking operating system: I-Wave.
In 1990 he started his first company for automated test systems.
During his army service developes an advanced neural network artificial creature, creating the theory of bottom-up artificial intelligence.
In the 90s starts a firm for the automated reproduction of ancient paintings by secret computer techniques.
From 1995 to 2002 Dominici is co-founder of the largest italian public network of computers.
From 2001 is president of Ultramundum Foundation.

Comments and Discussions

 
QuestionPerhaps it will fail for same object inside the object Pin
shashanksharma11-Sep-13 19:11
shashanksharma11-Sep-13 19:11 
AnswerRe: Perhaps it will fail for same object inside the object Pin
Fulvio Dominici12-Sep-13 0:10
Fulvio Dominici12-Sep-13 0:10 
GeneralGood one.. Pin
Nitin Singh India10-Sep-13 7:01
Nitin Singh India10-Sep-13 7:01 
GeneralRe: Good one.. Pin
Fulvio Dominici12-Sep-13 0:12
Fulvio Dominici12-Sep-13 0:12 
GeneralMy vote of 5 Pin
Jan Zumwalt10-Sep-13 5:44
Jan Zumwalt10-Sep-13 5:44 
GeneralRe: My vote of 5 Pin
Fulvio Dominici12-Sep-13 0:11
Fulvio Dominici12-Sep-13 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.