Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Javascript
Article

How to Inspect a JavaScript Object

Rate me:
Please Sign up or sign in to vote.
3.67/5 (10 votes)
21 Mar 2008GPL3 100.1K   14   5
List JavaScript Object Properties, ordered by levels

Introduction

This is a useful tool whenever you work with JavaScript and the compatibility issues between navigators.

You can inspect any JavaScript objects and list them as indented, ordered by levels.
It shows you type and property name. If an object property can't be accessed, an error message will be shown.

Using the Code

JavaScript
function inspect(obj [, maxLevels [, level]]) 

Input Vars

  • obj: Object to inspect
  • maxLevels: Optional. Number of levels you will inspect inside the object. Default MaxLevels=1
  • level: RESERVED for internal use of the function

Return Value

HTML formatted string containing all values of inspected object obj.

JavaScript
function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '<ul>';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '<li>(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += '</ul>';

    return str;
}

History

  • 22nd March, 2008: Article posted

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
Venezuela Venezuela
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBe Careful Pin
Revlin9-Oct-12 19:56
Revlin9-Oct-12 19:56 
GeneralNot bad ... Pin
Vertyg022-Mar-08 0:39
Vertyg022-Mar-08 0:39 
But I like firebug (firefox extension) more ...
Cheers!
GeneralRe: Not bad ... Pin
Ariel Tapia17-Jun-08 13:49
Ariel Tapia17-Jun-08 13:49 
GeneralRe: Not bad ... [modified] Pin
nickyt22-Jul-08 3:52
nickyt22-Jul-08 3:52 
General:) Pin
Ariel Tapia8-Nov-08 16:11
Ariel Tapia8-Nov-08 16: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.