Some days ago I was building a simple type detection function, maybe its useful for you:
Usage:
//...
if (typeString(obj) == 'array') {
//..
}
Implementation:
function typeString(o) {
if (typeof o != 'object')
return typeof o;
if (o === null)
return "null";
//object, array, function, date, regexp, string, number, boolean, error
var internalClass = Object.prototype.toString.call(o)
.match(/\[object\s(\w+)\]/)[1];
return internalClass.toLowerCase();
}
The second variant of this function is more strict, because it returns only object types described in the ECMAScript specification (possible output values: "object"
, "undefined"
, "null"
, and "function"
, "array"
, "date"
, "regexp"
, "string"
, "number"
, "boolean"
"error"
, using the [[Class]] internal property).