Last active
April 19, 2017 03:32
-
-
Save hcl1687/2c875dfa01c27635c449d5d1b77d90ed to your computer and use it in GitHub Desktop.
type
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
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