Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created January 16, 2009 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsmith/47997 to your computer and use it in GitHub Desktop.
Save lsmith/47997 to your computer and use it in GitHub Desktop.
JavaScript function to serve as a replacement for native typeof that returns more useful values. Example usage page at http://lucassmith.name/pub/typeof.html
var _toS = Object.prototype.toString,
_types = {
'undefined' : 'undefined',
'number' : 'number',
'boolean' : 'boolean',
'string' : 'string',
'[object Function]' : 'function',
'[object RegExp]' : 'regexp',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object Error]' : 'error'
};
// Function name can't be can't be typeof or typeOf because Safari barfs on
// the reserved word use. Non-IE browsers report the browser object classes
// in the toString e.g. '[object HTMLDivElement]', but IE always returns
// '[object Object]' for DOM objects and methods because they are COM objects
function type(o) {
return _types[typeof o] || _types[_toS.call(o)] || (o?'object':'null');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment