Foros del Web » Programando para Internet » Javascript »

Debug: var_dump

Estas en el tema de Debug: var_dump en el foro de Javascript en Foros del Web. Acabo de hacer un pequeño script de JS que sirve para debugs. Lo que hace es similar a la función de PHP var_dump, quiero escuchar ...
  #1 (permalink)  
Antiguo 30/09/2006, 09:05
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años, 1 mes
Puntos: 17
Debug: var_dump

Acabo de hacer un pequeño script de JS que sirve para debugs. Lo que hace es similar a la función de PHP var_dump, quiero escuchar sus opinions (y aportes, especialmente :P).

Soporta strings, images, numbers, arrays, dates, booleanos.

Código PHP:
<html>
<
head>
    <
title>Javascript var_dump</title>
    <
script language="javascript" type="text/javascript">
    
Object.prototype.var_dump = function (prefix) {
        var 
var_dump "";
        if (
prefix == undefined) { prefix ""; }
        if (
this.type == "image") return prefix "Image ('" this.src+"')\n";
        if (
this.type == "date") return prefix "Date ('" this.toString() + "')\n";
        if (
typeof this != "string" && typeof this != "boolean" && typeof this != "number" && !this.sort) return prefix "Unsoported type ("this +")\n";
        
var_dump += prefix 'array(' this.length ') {\n';
        
prefix += "\t";
        for (
key in this) {
          var 
value this[key];
          if (!
value.var_dump) { continue; }
          if (
typeof value == 'function' || (value == this.type && key == "type")) continue;
          
var_dump += prefix '['+writeKey(key)+'] =>\n' value.var_dump(prefix);
        }
        
var_dump += prefix.substring(0,prefix.length 1) + '}\n';
        return 
var_dump;
    }
    
    
Image.prototype.type 'image';
    
Date.prototype.type 'date';
    Array.
prototype.type 'array';
    
    
String.prototype.var_dump = function(prefix) {
        if (
prefix == undefined) { prefix ""; }
        return 
prefix 'string(' this.length ') "' this '"\n';
    }
    
Boolean.prototype.var_dump = function(prefix) {
        if (
prefix == undefined) { prefix ""; }
        return 
prefix 'bool(' + (this == true) + ') \n';
    }
    
    
Number.prototype.var_dump = function(prefix) {
        if (
prefix == undefined) { prefix ""; }
          if (
this.isInt())
              return 
prefix 'int(' this ')\n';
          else
              return 
prefix 'float(' this ')\n';
    }
    
    
Number.prototype.isInt = function (number) {
          if (
parseInt(this) == this)
              return 
true;
          return 
false;
    }

    function 
writeKey(key) {
        var 
toReturn "";
          if (
parseFloat(key) != keytoReturn += '"';
          
toReturn += key
          
if (parseFloat(key) != keytoReturn += '"';
          return 
toReturn;
    }
    
    
    
    var 
algo = new Array(new Image(), new Date(), 'hola'true11.4, new Option());
    
</script>
</head>
<body>
<button id="gola" style="width:200px; height:20px;" onclick="document.getElementById('target').innerHTML = (algo.var_dump())">Var_dump</button>
<p>Este script emula la funcion var_dump de PHP. Es un metodo que soporta strings, images, numbers, arrays, dates, booleanos.</p>
<p>Variable definida como "var algo = new Array(new Image(), new Date(), 'hola', true, 1, 1.4, new Option());"</p>
<pre id="target"></pre>
</body>
</html> 
  #2 (permalink)  
Antiguo 30/09/2006, 15:59
Avatar de Cap.Buscapina  
Fecha de Ingreso: octubre-2004
Ubicación: Argentina
Mensajes: 836
Antigüedad: 19 años, 6 meses
Puntos: 4
al parecer en IE y oprea no funcionan, si en Firefox.

saludos
__________________
by Capitán Buscapina
.
  #3 (permalink)  
Antiguo 02/10/2006, 17:32
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años, 1 mes
Puntos: 17
Es cierto, cap.
Probando ahora en IE veo que no soporta atributos (¿solo atributos?) en los prototipos de ¿ciertos? objetos.

Eliminando los tres renglones
Image.prototype.type = 'image';
Date.prototype.type = 'date';
Array.prototype.type = 'array';

funciona, pero sin soporte de imagenes. Los arrays y las fechas si las soporta. Quiero mejorar en ese sentido.
  #4 (permalink)  
Antiguo 28/01/2008, 20:58
 
Fecha de Ingreso: enero-2008
Ubicación: Lima
Mensajes: 127
Antigüedad: 16 años, 3 meses
Puntos: 0
Re: Debug: var_dump

PARA QUE SIRVE EL

var_dump($c);

EN PHP
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:15.