の試み:
デモを参照してください:http://jsbin.com/oxeki
コード:
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;
};
})();
私はこのようなかわいいプリンターが循環参照のためにどのように働くのだろうかと興味深い質問があります... –