如何在Chrome控制台中显示完整对象?

时间:2010-12-19 12:34:24

标签: javascript debugging google-chrome

var functor=function(){
    //test
}

functor.prop=1;

console.log(functor);

这只显示了仿函数的函数部分,无法在控制台中显示仿函数的属性。

9 个答案:

答案 0 :(得分:226)

使用console.dir()输出您可以点击的可浏览对象,而不是.toString()版本,如下所示:

console.dir(functor);
  

打印指定对象的JavaScript表示形式。如果要记录的对象是HTML元素,则其DOM表示的属性将打印 [1]


[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir

答案 1 :(得分:111)

如果你尝试,可能会得到更好的结果:

console.log(JSON.stringify(functor));

答案 2 :(得分:13)

如果你尝试,你可能会得到更好的结果:

console.log(JSON.stringify(obj, null, 4));

答案 3 :(得分:9)

var gandalf = {
  "real name": "Gandalf",
  "age (est)": 11000,
  "race": "Maia",
  "haveRetirementPlan": true,
  "aliases": [
    "Greyhame",
    "Stormcrow",
    "Mithrandir",
    "Gandalf the Grey",
    "Gandalf the White"
  ]
};
//to console log object, we cannot use console.log("Object gandalf: " + gandalf);
console.log("Object gandalf: ");
//this will show object gandalf ONLY in Google Chrome NOT in IE
console.log(gandalf);
//this will show object gandalf IN ALL BROWSERS!
console.log(JSON.stringify(gandalf));
//this will show object gandalf IN ALL BROWSERS! with beautiful indent
console.log(JSON.stringify(gandalf, null, 4));

答案 4 :(得分:8)

这对我很有用:

for(a in array)console.log(array[a])

您可以提取在控制台中创建的任何数组,以便查找/替换清除和后续使用此数据提取

答案 5 :(得分:0)

我写了一个函数来方便地将东西打印到控制台。

// function for debugging stuff
function print(...x) {
    console.log(JSON.stringify(x,null,4));
}

// how to call it
let obj = { a: 1, b: [2,3] };
print('hello',123,obj);

将在控制台中输出:

[
    "hello",
    123,
    {
        "a": 1,
        "b": [
            2,
            3
        ]
    }
]

答案 6 :(得分:0)

在现代浏览器中,console.log(functor)可以完美工作(因为与console.dir相同)。

答案 7 :(得分:0)

我做了Trident D'Gao答案的功能。

function print(obj) {
  console.log(JSON.stringify(obj, null, 4));
}

使用方法

print(obj);

答案 8 :(得分:-3)

输出obj:

console.log(obj, null, 4)
相关问题