Javascript 如何在 Chrome 控制台中显示完整对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4482950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show full object in Chrome console?
提问by lovespring
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.
这仅显示函子的功能部分,不能在控制台中显示函子的属性。
回答by Nick Craver
Use console.dir()
to output a browse-able object you can click through instead of the .toString()
version, like this:
使用console.dir()
输出可浏览的对象,你可以点击,而不是通过.toString()
的版本,就像这样:
console.dir(functor);
Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed [1]
打印指定对象的 JavaScript 表示。如果记录的对象是 HTML 元素,则打印其 DOM 表示的属性[1]
[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir
[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir
回答by BastiBen
You might get better results if you try:
如果您尝试以下方法,您可能会获得更好的结果:
console.log(JSON.stringify(functor));
回答by Trident D'Gao
You might get even better results if you try:
如果您尝试以下方法,您可能会获得更好的结果:
console.log(JSON.stringify(obj, null, 4));
回答by Kean Amaral
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));
回答by domSurgeon
this worked perfectly for me:
这对我来说非常有效:
for(a in array)console.log(array[a])
you can extract any array created in console for find/replace cleanup and posterior usage of this data extracted
您可以提取在控制台中创建的任何数组,用于查找/替换清理和提取的此数据的后验使用
回答by akim
With modern browsers, console.log(functor)
works perfectly (behaves the same was a console.dir
).
使用现代浏览器,console.log(functor)
可以完美运行(行为与 a 相同console.dir
)。
回答by Jens T?rnell
I made a function of the Trident D'Gao answer.
我做了一个函数的 Trident D'Gao 答案。
function print(obj) {
console.log(JSON.stringify(obj, null, 4));
}
How to use it
如何使用它
print(obj);
回答by John Henckel
I wrote a function to conveniently print things to the console.
我编写了一个函数来方便地将内容打印到控制台。
// 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);
will output in console:
将在控制台输出:
[
"hello",
123,
{
"a": 1,
"b": [
2,
3
]
}
]
回答by Kulakov Serg
To output obj:
输出 obj:
console.log(obj, null, 4)