var functor=function(){
//test
}
functor.prop=1;
console.log(functor);
这只展示了函数部分的函数对象,在控制台中无法展示函数对象的属性。
var functor=function(){
//test
}
functor.prop=1;
console.log(functor);
这只展示了函数部分的函数对象,在控制台中无法展示函数对象的属性。
使用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));
如果你尝试以下方法,可能会获得更好的结果:
console.log(JSON.stringify(obj, null, 4));
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));
for(a in array)console.log(array[a])
for (i in arr) { console.log(i); console.log(arr[i]); }
这段代码使用 JavaScript 编写,它使用 for 循环语句遍历一个数组中的所有元素。对于数组 arr 中的每个元素,该循环将会输出两行信息。第一行包含该元素在数组中的索引 i,第二行则包含该元素的值 arr[i]。通过这种方式,程序员可以轻松访问和处理数组中的所有元素。 - Geofunction print(obj) {
console.log(JSON.stringify(obj, null, 4));
}
如何使用它
print(obj);
console.log( [functor] );
在现代浏览器中,console.log(functor)
可以完美地工作(与console.dir
的行为相同)。
// 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
]
}
]