How to get the class of an object value

When developing in JavaScript, I often hear people saying, that they need a reliable way of determining of which type a value is. They want to know, with good reason, if they are dealing with an Array or a NodeList or a plain Object, or maybe if it is simply Null. The problem turns out to be twofold; one being, that they are asking the wrong question in relation to what they want to know. And two, to find the answer to the right question requires a small detour. First lets look at why it is the wrong question to ask.

Everything is objects, so they have classes

The reason the question is wrong is, that what they really want to know, is which object class their given value is, not what type it is. As everything is objects in JavaScript, anything that is not specified specifically in the ECMA-262 standard to do otherwise, will be of type object. The type object covers all the different value types available in the language, and each of them have their own class name. For example, arrays in JavaScript are just plain objects with some additional methods available, which is why when you retrieve the type of an array, you get "object". However, the object class of arrays is Array. Another example is Strings, which is specifically set to return type "string" when called with the typeof operator. But Strings also belongs under the object type and there it has the object class String. So in short, what they really are asking, and what is interesting and relevant to know is, which object class the value has. Now that we have that established, it seems easy enough; get the values object class and we have everything we need. So how do we do that?

Getting the object class

As it is at the moment, the object class of a given value is only directly accessible internally in the language, which is a bit inconvenient. This means that we only can get it indirectly, through methods that reveal it as part of the solution to something else. The method that we are going to use is the Object.prototype.toString() method, which returns a String that is put together like this; (this definition is taken form the ECMAScript 5.1 standard section 15.2.4.2)

  1. If the this value is undefined, return "[object Undefined]".
  2. If the this value is null, return "[object Null]".
  3. Let O be the result of calling ToString passing the this value as the argument.
  4. Let class be the value of the [[Class]] internal property of O.
  5. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

As we can see, the second parameter inside the [] brackets is the class of the given value. The values undefined and null get their classes hard coded into the spec, but for all other values the class is retrieved via the internal property [[Class]] of the value.

Writing the isObjClass method

With the knowledge we have now, it is relatively easy to write a method to test if a given value belongs to a specific class.

  1. var isObjClass = function (o,ocl) {
  2. return Object.prototype.toString.call(o) === '[object ' + ocl + ']';
  3. };

As you can see, the method takes two parameters, o which is the object we want to test, and ocl which is the object class we want to test against. Of course you can write this method any way you want, like as returning the object class instead of testing against it, but I find this specific approach very useful.