Click here to Skip to main content
6,594,432 members and growing! (16,391 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The GNU General Public License (GPL)

How to Inspect a JavaScript Object

By Ariel Tapia

List JavaScript Object Properties, ordered by levels
Javascript, Dev
Posted:22 Mar 2008
Views:15,245
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.67 Rating: 3.67 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2
1 vote, 10.0%
3
1 vote, 10.0%
4
6 votes, 60.0%
5

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

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.

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 (GPL)

About the Author

Ariel Tapia


Member

Occupation: Web Developer
Location: Venezuela Venezuela

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralNot bad ... PinmemberVertyg01:39 22 Mar '08  
GeneralRe: Not bad ... PinmemberAriel Tapia14:49 17 Jun '08  
GeneralRe: Not bad ... [modified] Pinmembernickyt4:52 22 Jul '08  
General:) PinmemberAriel Tapia17:11 8 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Ariel Tapia
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project