Last active
February 15, 2016 16:12
-
-
Save WebReflection/6323064 to your computer and use it in GitHub Desktop.
An incrementally improved approach to a common JavaScript function/utility widely adopted all over. Returns the lowercase type or [[Class]] of a generic variable.
This file contains 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 typeOf = (function(Object, RegExp){ | |
// WTFPL License - http://en.wikipedia.org/wiki/WTFPL | |
// thanks to @jdalton and @ljharb | |
var toString = Object.prototype.toString, | |
cache = (Object.create || Object)(null); | |
return function typeOf(Unknown) { | |
var asString = typeof Unknown; | |
return asString == 'object' ? ( | |
Unknown === null ? 'null' : ( | |
cache[asString = toString.call(Unknown)] || ( | |
cache[asString] = asString | |
.slice(asString.indexOf(' ') + 1, -1) | |
.toLowerCase() | |
) | |
) | |
) : asString; | |
}; | |
}(Object, RegExp)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's Special About This Code
int32array
,htmlcanvaselement
, any other available type)