JavaScript Get Object Type
Object.prototype.toString( ) When the toString method is called, the following steps are taken:
- Get the [[Class]] property of this object.
- Compute a string value by concatenating the three strings “[object ", Result (1), and "]“.
- Return Result (2)
So, with that out of the way, the best option we have to get an object's type is something like the following:
var getType = function(obj){
return ({}.toString.call(obj)).replace('[Object ','').replace(']','');
//.match(/^\[object\s(.*)\]$/)[1]
};
Is a solid, cross-browser implementation and cross frame safe.