13 сентября 2009-го

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

13 сентября 2009, 15:17

Javascript advancent dump

Хорошо для кастомных объектов, то есть для отладки бизнес-логики, но не ДОМ.
Object.prototype.dump = function( excludePrototype, maxDepth, depth ){
    var output = [ "" ], key = "", indent = "";
    
    depth    = depth || [];
    maxDepth = maxDepth >= 0 ? maxDepth : -1;
    excludePrototype = !!excludePrototype;
    
    for( var i = 0; i < depth.length; i++ ) indent += "  ";
    if( maxDepth === -1 || depth.length < maxDepth ){
        for( key in this ) 
        	if( ( this.hasOwnProperty( key ) || excludePrototype ) ){
            if( depth.contains( this[ key ] ) ){
              output[ 0 ] = "{circular reference}";
            } else {
                depth.push( this[ key ] );
                output.push( 
                    indent + key + ": " + 
                    this[ key ].dump( excludePrototype, maxDepth, depth ) 
                );
                depth.pop( this[ key ] );
            }    
        }
    } else output[ 0 ] = "{object}";
    return output.join("\n");
};
Array.prototype.contains = function( obj ){
    for( var i = 0; i < this.length; i++ ){
        if( this[ i ] === obj ){
            return true; }
    }
    return false;
};
// default dumpster method
var defaultDumpster = function(){ return this.toString(); };
String.prototype.dump =
Number.prototype.dump =
RegExp.prototype.dump =
Boolean.prototype.dump = defaultDumpster;
Function.prototype.dump = function(){ return "{function}"; };
Date.prototype.dump = function(){ return "{" + this.getTime() + "}"; };
alert(obj.attributes.dump());

Распарсить атрибуты ДОМелемента, можна используя простой скрипт
var elm = obj.attributes;
for( var x = 0; x < elm.length; x++ ) 
	document.write(elm[x].name + ' ' + elm[x].nodeValue + '</br>');


9 сентября 2009-го  . . .       Ctrl       . . .  17 сентября 2009-го