Created
January 16, 2009 16:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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