Get All Properties of an Object in JavaScript

I often have the need to find all of the properties of an object in order to determine what is available to work with. In this post, I’ll show you a handy function that you can use to provide this information.

As I worked through many of the posts in the Dojo Data Grid series, I was routinely slowed down by the lack of detailed documentation on many of the grid and plugin event properties. This function was very useful in inspecting the properties and giving me ideas for new approaches to try.

Fortunately, JavaScript makes it easy to inspect an object and display all of its properties. This function can be called with an object parameter and it will display a list of properties of the object.

function getAllProperties(obj) {
  var properties = '';
  for (property in obj) {
    properties += '\n' + property;
  }
  alert('Properties of object:' + properties);
}
Advertisements

5 responses to “Get All Properties of an Object in JavaScript”

  1. MarkyRoden says :

    Using Firebug console.dir(obj) is another really easy way to see everything about an object

  2. Shawn Sweeney says :

    You can also use typeof to check the properties to see if they are functions or nexted objects if you’re only interested in properties not all members.
    example: if(typeof obj[property] != “function” && typeof obj[property] != “object”)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: