javascript property dump

I’ve found it quite useful sometimes to do a dump of properties of a javascript object. Here’s an example for an event:

var alerttext = "";
for(p in myEvent)
{
	if(p.toUpperCase() != p && (typeof(myEvent[p]) == 'string' || typeof(myEvent[p]) == 'number'))
	{
		alerttext += p + " = " + myEvent[p] + "\n";
	}
}
alert(alerttext);

This uses the nice javascript for ... in statement. It does a few checks on the properties to figure out which are ‘interesting’. Properties with names all in upper case are assumed to be constants, so we don’t bother with those. Otherwise we dump the ones that are simple strings or numbers. Sometimes I might add functions to this, but they tend to get a bit long for an alert box.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*