Dec 29 2009
Javascript print_r
This is my own javascript version of the PHP’s print_r function, which is used to print the properties of an object with its values.
Download
This is the function, and below you can find the explanation.
function print_r(obj, tab, depth){
var ignorezero= true;
var ignorefunction= true;
var ignorenull= true;
var ignoremaxreached = true;
var output = tab + obj + ":\n";
try {
tab += " - ";
for (var childname in obj){
var func= 0;
childval= obj[childname];
if (childval==null){
if (ignorenull) continue;
else childstr= "null";
}else if ((childval==0 || childval=="") && ignorezero )
continue;
else
childstr= childval.toString();
if (childstr.length>8)
if (childstr.substring(0,8)=="function")
func= 1;
if (func){
if (!ignorefunction)
output += tab + childname +": function\n";
}else if (typeof childval=="object"){
if (childval != null){
if (depth==0)
output += tab + childname + ": " +childstr + "\n";
else
output += tab + childname +": "+print_r(childval,tab,depth-1)+"\n";
}else if(!ignorenull)
output += tab + childname +": null\n";
}else{
output += tab + childname +": "+ childstr + "\n";
}
}
}catch(exc){
}
return output;
}
So here it goes slowly:
function print_r(obj, tab, depth){
- obj: This is the object, array, variable you want to print, examples: document.form, ajaxresponse, etc…
- tab: most of the times it will be: “”, it’s just the tab spacing you want to use
- depth: how many levels it should dig, for example, if you want to print “document”, it can go to: document.form.input.value which will be the 4th level
var ignorezero= true;
var ignorefunction= true;
var ignorenull= true;
var ignoremaxreached = true;
These are some control variables you can customize depending on how much information you want to have.
- ignorezero: if the value of a property is “” or 0 don’t print it
- ignorefunction: do not print methods
- ignorenull: if the property is another object equals to null, do not print it
- ignoremaxreached: once the max depth has been reached and an object is found, ignore it
var output = tab + obj + ":\n";
try {
tab += " - ";
output is the variable used to store the properties, we use tab for spacing and in this case the toString() method of the obj.
Once we saved the name of the object we need to indent the properties, that’s why we use the tab variable.
for (var childname in obj){
var func= 0;
childval= obj[childname];
This is just the loop thru the properties of the object. func is another control variable to know childname is a method (function) or not. childval is the actual property.
if (childval==null){
if (ignorenull) continue;
else childstr= "null";
}else if ((childval==0 || childval=="") && ignorezero )
continue;
else
childstr= childval.toString();
Ignore basic cases, and store the string value of the property.
if (childstr.length>8)
if (childstr.substring(0,8)=="function")
func= 1;
if (func){
if (!ignorefunction)
output += tab + childname +": function\n";
}
This is the way I know if a property is a method, if you have something better just let me know.
else if (typeof childval=="object"){
if (childval != null){
if (depth==0)
output += tab + childname + ": " +childstr + "\n";
else
output += tab + childname +": "+print_r(childval,tab,depth-1)+"\n";
}else if(!ignorenull)
output += tab + childname +": null\n";
}
If the property is another object ask if see if we loop into it or just print it’s name. Also ignore if it is null.
else{
output += tab + childname +": "+ childstr + "\n";
}
}
}catch(exc){
}
return output;
}
If the property is not an object or a function just print it.
Exception are ignored, and just return the output.
No responses yet