我使用以下函数在 Firefox 控制台日志中显示读数:
//// make printable string for console readout, recursively
var make_printable_object = function(ar_use)
{
//// internal arguments
var in_tab = arguments[1];
var st_return = arguments[2];
//// default vales when applicable
if (!in_tab) in_tab = 0;
if (!st_return) st_return = "";
//// add depth
var st_tab = "";
for (var i=0; i < in_tab; i++) st_tab = st_tab+"-~-~-";
//// traverse given depth and build string
for (var key in ar_use)
{
//// gather return type
var st_returnType = typeof ar_use[key];
//// get current depth display
var st_returnPrime = st_tab+ "["+key+"] ->"+ar_use[key]+"< is {"+st_returnType+"}";
//// remove linefeeds to avoid printout confusion
st_returnPrime = st_returnPrime.replace(/(\r\n|\n|\r)/gm,"");
//// add line feed
st_return = st_return+st_returnPrime+"\n";
//// stop at a depth of 15
if (in_tab>15) return st_return;
//// if current value is an object call this function
if ( (typeof ar_use[key] == "object") & (ar_use[key] != "null") & (ar_use[key] != null) ) st_return = make_printable_object(ar_use[key], in_tab+1, st_return);
}
//// return complete output
return st_return;
};
例子:
console.log( make_printable_object( some_object ) );
或者,您可以替换:
st_return = st_return+st_returnPrime+"\n";
和
st_return = st_return+st_returnPrime+"<br/>";
在 html 页面中打印出来。