How to inspect Javascript Objects

后端 未结 8 534
半阙折子戏
半阙折子戏 2020-12-07 10:31

How can I inspect an Object in an alert box? Normally alerting an Object just throws the nodename:

alert(document);

But I want to get the p

相关标签:
8条回答
  • 2020-12-07 10:42

    Use your console:

    console.log(object);
    

    Or if you are inspecting html dom elements use console.dir(object). Example:

    let element = document.getElementById('alertBoxContainer');
    console.dir(element);
    

    Or if you have an array of js objects you could use:

    console.table(objectArr);
    

    If you are outputting a lot of console.log(objects) you can also write

    console.log({ objectName1 });
    console.log({ objectName2 });
    

    This will help you label the objects written to console.

    0 讨论(0)
  • 2020-12-07 10:42

    This is blatant rip-off of Christian's excellent answer. I've just made it a bit more readable:

    /**
     * objectInspector digs through a Javascript object
     * to display all its properties
     *
     * @param object - a Javascript object to inspect
     * @param result - a string of properties with datatypes
     *
     * @return result - the concatenated description of all object properties
     */
    function objectInspector(object, result) {
        if (typeof object != "object")
            return "Invalid object";
        if (typeof result == "undefined")
            result = '';
    
        if (result.length > 50)
            return "[RECURSION TOO DEEP. ABORTING.]";
    
        var rows = [];
        for (var property in object) {
            var datatype = typeof object[property];
    
            var tempDescription = result+'"'+property+'"';
            tempDescription += ' ('+datatype+') => ';
            if (datatype == "object")
                tempDescription += 'object: '+objectInspector(object[property],result+'  ');
            else
                tempDescription += object[property];
    
            rows.push(tempDescription);
        }//Close for
    
        return rows.join(result+"\n");
    }//End objectInspector
    
    0 讨论(0)
  • 2020-12-07 10:44

    Use console.dir(object) and the Firebug plugin

    0 讨论(0)
  • 2020-12-07 10:54
    var str = "";
    for(var k in obj)
        if (obj.hasOwnProperty(k)) //omit this test if you want to see built-in properties
            str += k + " = " + obj[k] + "\n";
    alert(str);
    
    0 讨论(0)
  • 2020-12-07 10:54

    Here is my object inspector that is more readable. Because the code takes to long to write down here you can download it at http://etto-aa-js.googlecode.com/svn/trunk/inspector.js

    Use like this :

    document.write(inspect(object));
    
    0 讨论(0)
  • 2020-12-07 10:57

    There are few methods :

     1. typeof tells you which one of the 6 javascript types is the object. 
     2. instanceof tells you if the object is an instance of another object.
     3. List properties with for(var k in obj)
     4. Object.getOwnPropertyNames( anObjectToInspect ) 
     5. Object.getPrototypeOf( anObject )
     6. anObject.hasOwnProperty(aProperty) 
    

    In a console context, sometimes the .constructor or .prototype maybe useful:

    console.log(anObject.constructor ); 
    console.log(anObject.prototype ) ; 
    
    0 讨论(0)
提交回复
热议问题