Design & Download
Your JS Library
in Seconds!!!

Snippet: isClass()

Written by Christopher West (cwest) on October 21, 2015.
Determines whether or not a function is a prototypal class-like function.
function isClass(o) {
  if (typeOf(o, 'Function') && (o = o.prototype)) {
    for (var i in o) {
      if (has(o, i)) {
        return true;
      }
    }
    return has(o, 'toString');
  }
  return false;
}

Firstly, even though I use the word class, I am actually talking about prototypes. Secondly, this solution is actually incomplete because it is very difficult to determine whether or not a function is class constructor. This solution simply tests to see if the passed in variable is a class that has custom prototypal members or if it has a custom prototypal member called toString.

One example case in which this function fails is when calling it for a constructor such as ArrayBuffer. Due to the fact that ArrayBuffer uses the same toString() method as Array, calling isClass(ArrayBuffer) returns false.

Another rare case in which this function will incorrectly return false is in the case that the class constructor passed has no custom prototypal members defined.

In the end, due to the fact that all JavaScript classes are actually functions it is not possible to write a function which truly determines in all cases if something is a class or not. On the other hand, this simple solution should work for almost any case that you would need to use it. Let me know if you think there is a way to improve it.