Expose an object’s properties in Javascript

Here’s another quickie in Javascript: Suppose you’ve got an object, but you’re not sure what properties it might have. This snippet will loop through an objects properties, at which point you can pop the results into an alert box, or whatever your favorite method of looking at debugging text might be…

var output_text = '';
for (var myprop in myobject){
output_text+=myprop+' ';
}

I have to admit, I use this all the time. So much, that here’s the same thing written as a function that you can just pass your object to, and have an alert box pop up with that object’s properties:

function expose(someobj) {
var output_text = '';
for (var myprop in someobj) {
output_text+=myprop+"n";
}
alert(output_text);
}

Leave a Reply