我知道firebug中的console.log,这个东西叫做dbug(但根本不是我想要的)。我正在寻找的东西将给我一个漂亮的打印嵌套视图到一个对于javascript看起来像这样的对象:
(来源:ospinto.com)
我也知道这个问题,我正在寻找一些更具表格的东西。
发布于 2019-03-21 09:57:06
代码:
var prettyPrint = (function(){
var htmlObj = function(obj){
if (Object.prototype.toString.call(obj) === '[object RegExp]') {
return obj.toSource ? obj.toSource() : '/' + obj.source + '/';
}
if (typeof obj === 'object') {
return prettyPrint(obj);
}
if (typeof obj === 'function') {
return document.createTextNode('function(){...}');
}
return obj.toString();
},
row = function(cells, type){
type = type || 'td';
var r = document.createElement('tr');
for (var i = 0, l = cells.length; i < l; i++) {
var td = document.createElement(type);
td.appendChild(typeof cells[i] === 'string' ? document.createTextNode(cells[i]) : cells[i]);
r.appendChild(td);
}
return r;
},
heading = function() {
var thead = document.createElement('thead');
thead.appendChild(row(['Name','Value'], 'th'));
return thead;
};
return function(obj) {
var tbl = document.createElement('table'),
tbody = document.createElement('tbody');
for (var i in obj) {
var objCellContent = obj[i] === obj ? document.createTextNode('CIRCULAR REFERENCE') : htmlObj(obj[i]);
tbody.appendChild( row([document.createTextNode(i), objCellContent]) );
}
tbl.appendChild(heading());
tbl.appendChild(tbody);
return tbl;
};
})();
https://stackoverflow.com/questions/-100008982
复制相似问题