Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Object debugger

  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object debugger

    This is a script that frequently keeps me from using bad words.


    It loops through an object and returns its properties in text (for alert) or html format.

    Usage:
    _viewObj(obj[,instanceName[,type[,levels]]]);

    Ex:
    var obj = {id:3,arr:[2,3,"str",{prop1:2,prop2:""}],nr:3}

    alert(_viewObj(obj));
    document.write(_viewObj(obj,"obj","html"));

    Parameters:
    pValue: The object
    pName: Instance name of the object. Default: "input"
    pType: Which type to be return (text or html). Default: "text"
    pLevels: The max amount of levels. Useful if an object references a parent. Default: infinite

    Returns
    A string with the properties

    Code:
    function _viewObj(pValue,pName,pType,pLevels,pObj) {
    
        function objExists(pCurr) {
            for (var i=0; i<pObj.length; i++) {
                if (pObj[i]==pCurr) {
                    return true;
                }
            }
            return false;
        }
    
    
        pType = new String(pType).toLowerCase();
        if (new String(pName) == "undefined")
            pName = "input";
        if (new String(pType).search(/html|text/i) == -1)
            pType = "text";
        if (pLevels == "undefined")
            pLevels = -1;                                    /* Infinite */
        if (typeof pObj == "undefined")
            pObj = [];
        var str="";
        if (typeof pValue != "function") {                                    // No functions / methods - just values
            var br=(pType == "text"? "\n" : (pType == "html" ? "<br>" : ""));
            if ((typeof pValue == "object" && !!pValue) && (pValue.length) && (!pValue.substring)) { // An array
                try {
                    for (var i=0;i<pValue.length;i++) {
                        if (pLevels==0) {
                            str+=br+pName+"["+i+"]: "+pValue;
                        }else{
                            str+=""+_viewObj(pValue[ i ],pName+"["+i+"]",pType,pLevels-1,pObj);
                        }
                    }
                } catch (e) {
                    str+=br+pName+": "+pValue+" (Not an array)";
                }
            } else if (typeof pValue == "object" && !!pValue && (!pValue.substring)){    // An object
                try {
                    for (var prop in pValue) {
                        if (pLevels==0) {
                            str+=br+pName+"."+prop+": "+pValue;
                        }else{
                            if (typeof pValue[prop] == "object" && objExists(pValue[prop])) {
                                str+=br+pName+"."+prop+": [Parent Object]";
                            }else{
                                pObj.push(pValue);
                                str+=""+_viewObj(pValue[prop],pName+"."+prop,pType,pLevels-1,pObj);
                            }
                        }
                    }
                } catch (e) {
                    str+=br+pName+": "+pValue+" (Not an object)";
                }
            } else{// if (typeof pValue != "object") {                                // Simple value
                str+=br+pName+": "+pValue;
            }
        }
        return str;
    }
    edit:
    Avoiding never-ending loops by checking for parent objects.

    Ex:
    var parentObj={id: 2};
    var test={id: 1,par: parentObj};
    parentObj.tst=test;
    Last edited by Danne; 11-05-2003 at 12:49 PM.
    /Daniel

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks pretty good.

    Mine has a hardcoded limit of five, due to IE crashing on more than the simplest nested objects.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it seems your has a better support for DOM elements, by avoiding some properties.

    IE takes forever when I tried to run a <td> element in 2 levels on mine. Mozilla had no problem with that, but crashed on 3 levels.
    /Daniel

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Huh? I made one that built the entire tree from window up in moz, making sure to eliminate infinite loops by cheching whether an object has been expanded before, and if so just referring to the first instance. It didn't break. Ie crashed because of some non-JScript objects, but op5 (which was the newest opera at that time) handled it just fine.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i'd like to see that, if i could.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good suggestion liorean!

    I added a check for parent objects. Unfortunately it didn't work with DOM anyway. If you can see what's wrong, you are most welcome to share that information...
    /Daniel

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Ï'll see if I can find it in my archives. That was on my previous machine...
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •