Skip to content

Instantly share code, notes, and snippets.

@hcl1687
Last active April 19, 2017 03:32
Show Gist options
  • Save hcl1687/2c875dfa01c27635c449d5d1b77d90ed to your computer and use it in GitHub Desktop.
Save hcl1687/2c875dfa01c27635c449d5d1b77d90ed to your computer and use it in GitHub Desktop.
type
const types = 'Boolean Number String Function Array Date RegExp Object Error'.split(' ')
const class2type = {}
types.forEach(name => {
class2type['[object ' + name + ']'] = name.toLowerCase()
})
function type (obj) {
if (obj === null) {
return obj + ''
}
return typeof obj === 'object' || typeof obj === 'function'
? class2type[Object.prototype.toString.call(obj)] || 'object' : typeof obj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment