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

lov*_*ing 145 javascript debugging google-chrome

var functor=function(){
    //test
}

functor.prop=1;

console.log(functor);
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*ver 233

使用console.dir()输出可浏览的对象,你可以点击,而不是通过.toString()的版本,就像这样:

console.dir(functor);
Run Code Online (Sandbox Code Playgroud)

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


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


Bas*_*Ben 116

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

console.log(JSON.stringify(functor));
Run Code Online (Sandbox Code Playgroud)

  • 出于对这个答案的所有应有的尊重,最终它返回一个表示对象的字符串,而不是控制台中的“可浏览”对象,就像问题就在这里一样。没错,如果你通过 JSON.parse 运行这个输出字符串,它会返回到它的对象格式,但是控制台仍然会显示一个“.toString()”,我们又回到了原点。此处使用“console.dir”的答案最适合手头的问题。 (3认同)

Tri*_*Gao 18

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

console.log(JSON.stringify(obj, null, 4));
Run Code Online (Sandbox Code Playgroud)


Kea*_*ral 12

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));
Run Code Online (Sandbox Code Playgroud)


小智 8

这对我很有用:

for(a in array)console.log(array[a])
Run Code Online (Sandbox Code Playgroud)

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

  • 更详细一点:`for(i in arr){console.log(i); (ARR [I])的console.log; }` (3认同)

Jen*_*ell 5

我做了一个 Trident D'Gao 答案的函数。

function print(obj) {
  console.log(JSON.stringify(obj, null, 4));
}
Run Code Online (Sandbox Code Playgroud)

如何使用它

print(obj);
Run Code Online (Sandbox Code Playgroud)