function getType(obj) {
if(obj && obj.constructor && obj.constructor.name) {
return obj.constructor.name;
}
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}
In my preliminary tests, this is working pretty well. The first case will print the name of any object created with "new", and the 2nd case should catch everything else.
I'm using (8, -1)
because I'm assuming that the result is always going to start with [object
and end with ]
but I'm not certain that's true in every scenario.