ah yes, woot. infact you are right.
It is better to roll the two ()?: into a one ifelse
and the switch is also a better choice. Since this code was constructed bottom up, and added on to as I went, it seemed easier to do things my way. But looking at the completed code top-down, your changes seem obvious (slap head)
🙂
I will also be using your updated version.
Since we are using a switch, we should have a default option that displays things stright, like a string... as well as a special option for the NULL value... but since null is an object, we can't have it's own switch case 🙁
<script type="text/javascript">
function print_r(input, _indent)
{
if(typeof(_indent) == 'string') {
var indent = _indent + ' ';
var paren_indent = _indent + ' ';
} else {
var indent = ' ';
var paren_indent = '';
}
switch(typeof(input)) {
case 'boolean':
var output = (input ? 'true' : 'false') + "\n";
break;
case 'object':
if ( input===null ) {
var output = "null\n";
break;
}
var output = ((input.reverse) ? 'Array' : 'Object') + " (\n";
for(var i in input) {
output += indent + "[" + i + "] => " + print_r(input[i], indent);
}
output += paren_indent + ")\n";
break;
case 'number':
case 'string':
default:
var output = "" + input + "\n";
}
return output;
}
var bar = new Object()
bar.foo = 'blue'
var foo = new Array()
foo['a'] = 1
foo['b'] = 2
var num = 3
var str = 'I am a string'
var nul = null
document.write('<pre>');
document.write( 'foo:'+ print_r(foo) );
document.write( 'bar:'+ print_r(bar) );
document.write( 'num:'+ print_r(num) );
document.write( 'str:'+ print_r(str) );
document.write( 'nul:'+ print_r(nul) );
document.write('</pre>');
</script>
my output was as follows
foo:Array (
[a] => 1
[b] => 2
)
bar:Object (
[foo] => blue
)
num:3
str:I am a string
nul:null