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) != key) toReturn += '"';
toReturn += key
if (parseFloat(key) != key) toReturn += '"';
return toReturn;
}
var algo = new Array(new Image(), new Date(), 'hola', true, 1, 1.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>