How to show full object in Chrome console

var functor=function(){
//test
}


functor.prop=1;


console.log(functor);

this only show the function part of the functor, cannot show the properties of the functor in console.

184576 次浏览

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

console.dir(functor);

打印指定对象的 JavaScript 表示形式。如果被记录的对象是一个 HTML 元素,那么它的 DOM 表示的属性将被打印为 [1]


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

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

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

这对我来说非常有效:

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

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

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

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

我编写了一个函数来方便地将内容打印到控制台。

// 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
]
}
]
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));

对于现代浏览器,console.log(functor)工作得非常完美(其行为与 console.dir相同)。

我用高三叉戟做了个函数。

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

怎么用

print(obj);

另一种方法是将函数包装到数组中:

console.log( [functor] );