Return to Snippet

Revision: 22777
at January 22, 2010 02:49 by Nick___


Initial Code
function dump(o,limit,e,clicked) {
  var outer = false;
  if (e===undefined) {
    dump.o=[];
    dump.n = function(o) { // get display name for object
      if (o===undefined) return '<i>undefined</i>';
      if (o===null) return '<i>null</i>';
      var c = o.constructor;
      return '<span style="color:#666"> <i>' + typeof o + '</i> '
        +(c ? String(c).split('{')[0].substr(8) :'') + '</span> '
        +'<pre style="display:inline">' + dump.esc(o) + '</pre>' }
    dump.hp = function(o) { // determine if object has props
      for (undefined in o) return true; return false; }
    dump.t = function(e,rhs) { // toggle property list viz
      var n = rhs ? e.previousSibling : e.nextSibling;
      var s = n.style;
      if ((!n.hasData) && s.display == 'none')
        return dump.load(n);
      n.hasData = true;
      s.display = s.display == 'none' ? 'block' : 'none';
      return false; }
    dump.load = function(e) { // show non-dumped item
      e.hasData = true;
      e.style.display = 'block';
      dump(dump.o[e.getAttribute('obj')],1,e,true);
      return false; }
    dump.esc = function(s) { // html escape
      return String(s).replace(/&/g,'&amp;')
        .replace(/</g,'&lt;').replace(/\>/g,'&gt;') }
    outer = true;
    e = document.createElement('div');
    e.style.font = '11px sans-serif'; }
  if (outer || clicked) {
    e.val = "";
    e.write = function(s) {e.val += s}
    e.flush = function() {e.innerHTML += e.val; e.val = ''} }
  if (limit===undefined) limit = 3;
  var tab = '<br/\><a href="#" onclick="return dump.t(this)">'
    +'<b>{</b></a><div style="margin-left:1em'
    +(limit ? '' : ';display:none') +'"'
    +(limit ? '' : 'obj="'+dump.o.length+'" ') +'>';
  var end = '</div><a href="#" onclick="return dump.t(this,1)">'
    +'<b>}</b></a><br/\>';
  if (!limit) {
    if (!dump.hp(o))
      return e.write(dump.n(o)+'<br/\>');
    dump.o.push(o);
    return e.write(dump.n(o)+tab+end); }
  if (!clicked) e.write(dump.n(o));
  var c = 0;
  for (var p in o) {
    if ((!clicked) && !c++) e.write(tab);
    e.write("<b>"+p+"</b> => ");
    dump(o[p], limit-1, e); }
  e.write(clicked ? "" : c ? end : "<br/\>");
  if (outer || clicked) e.flush();  
  if (outer) document.body.appendChild(e); }

Initial URL

                                

Initial Description

                                

Initial Title
Dump object properties (recursive)

Initial Tags

                                

Initial Language
JavaScript